In the next post in the Admin Tips series, we’ll cover the split program.
The split program splits files into parts. It outputs equal parts of FILE to the files PREFIXaa PREFIXab, …; the default size is 1000 lines, and the default PREFIX is x. If FILE is not specified or – is specified as FILE, it reads the standard input. The arguments that are required for long options are also required for short options.
The split program is part of the coreutils package.
Options
-a, –suffix-length=N : Use suffixes of length N (default 2)
–additional-suffix=SUFFIX : Add an additional SUFFIX to filenames
-b, –bytes=SIZE : Specify the size of each output file to SIZE
-C, –line-bytes=SIZE : Use at most SIZE bytes per record in each output file
-d : Use numeric suffixes starting at 0, not alphabetic
–numeric-suffixes[=FROM] : Like -d, but allows you to choose a starting value
-x : Use hex suffixes starting at 0, not alphabetic
–hex-suffixes[=FROM] : Like -x, but allows you to choose a starting value
-e, –elide-empty-files : Do not print empty lines for the -n option
–filter=COMMAND : Write to the shell COMMAND, the filename is $FILE
-l, –lines=NUMBER : use NUMBER lines/records for each output file
-n, –number=PARTS : split the file into the specified number of PARTS, see below
-s, –separator=SEPARATOR : use SEPARATOR instead of newline as the record separator; “\0” (zero) selects the NUL character
-u, –unbuffered : immediately copies input to output with the -n r/… option
–verbose : displays diagnostic information just before opening each output file
Syntax
split [options] filename new-file-prefix
Examples
Split file into two parts
split -n 2 file1
This will create two new files: xaa and xab
To split a file into two new files with their own names:
split -n 2 file1 file1.part
This will create two new files: file1.partaa
file1.partab
Split a file by line of text, e.g., every 5th line
split file1 -l 5
Split a file with displayed information about the actions
split -n 2 file1 –verbose
Split file1 (15 MB) with a maximum output file size of 5 MB
split –verbose -b 5MB file1 file.part
Which will result in three files: file1.partaa, file1.partab, file1.partac, each 5MB.
For more information about the split program, use the commands:
split –help
man split
