How to Divide Files with split
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
What is split?
split
is a Unix command-line utility similar to grep
or tail
. It allows you to divide a larger file into several smaller files.
NoteCertain options forsplit
will not work by default on macOS because the GNU version of split does not come pre-installed. Use Homebrew to installbrew install coreutils
then invoke in GNU split viagsplit
.
Example Files
Create
example.txt
in a text editor and add the following content:- File: example.txt
1 2 3 4 5 6 7 8 9 10
example line 1 example line 2 example line 3 example line 4 example line 5 example line 6 example line 7 example line 8 example line 9 example line 10
Download the text of Moby Dick to demonstrate working with larger files:
wget -O moby-dick.txt https://archive.org/stream/mobydickorwhale01melvuoft/mobydickorwhale01melvuoft_djvu.txt
Basic Usage
Run the
split
command with default options:split moby-dick.txt
Check your working directory:
ls
moby-dick.txt xaa xab xac xad xae xaf xag ...
The new files present in the directory (
xaa
,xab
, etc.) each contain a portion of the original file. By default,split
divides a file into subfiles of 1000 lines each. The originalmoby-dick.txt
file had 16,000 lines, resulting in 16 subfiles. The originalmoby-dick.txt
file is left unchanged.
Options and Parameters
Prefix
The first argument to split
is the name of the file, as demonstrated above. An optional second argument allows you to specify the prefix for the output files. By default, this value is x
.
split moby-dick.txt moby-dick
Each of the files will begin with moby-dick
.
moby-dick.txt moby-dickaa moby-dickab moby-dickac ...
Split by Number of Lines
The -l
option sets the length in lines of each subfile. This value is 1000 by default. The files output by the following command will each contain two lines of text:
split -l 2 example.txt
$ cat xaa
example line 1
example line 2
Split by Size
The -b
(or --size
) option divides files by size rather than number of lines. The following command will split the input file into subfiles of 100KB each:
split -b 100k moby-dick.txt
You can specify this value in different formats:
- megabytes - m
- gigabytes - g
- terabytes - t
Split by Number of Files
To split a file into a specific number of subfiles, regardless of size or length, use the -n
option. For example, to split a file into 3 parts:
split -n 3 example.txt
Label Files Numerically
Use the -d
option to label the output files numerically rather than alphabetically:
split -l 2 -d example.txt
x00 x01 x02 x03 x04
Set Suffix Length
Use the -a
option to set the number of digits or letters used when labeling the output files. This option defaults to two (i.e. x00
).
split -a 1 -d -l 2 example.txt
x0 x1 x2 x3 x4
Advanced Examples
The following command combines the options above to split example.txt
into 4 subfiles, each prefixed with example-
and labeled numerically:
split -a 1 -n 4 -d example.txt example-
example-0 example-1 example-2 example-3 example.txt
split
can also be used to display portions of files without creating subfiles. The following command will break Moby Dick into 100 pieces (without creating any new files) and display the 10th of those pieces:
split -n 10/100 moby-dick.txt
Like many shell commands, split
can also accept input from the output of another command using the pipe operator:
grep whale moby-dick.txt | split -l 100
This page was originally published on