split command (splits a file)
- Organization of this page
Format
split [-a suffix-length] [-b num-bytes [k|m]|-l num-lines] [input-path-name [prefix]]
Description
This command splits the contents of a file or of the standard input into segments and outputs the segments to separate files.
Arguments
- -a suffix-length
-
Specifies the length of the suffix that is to be appended to the resulting file names after the input file is split.
Specify a value in the range 1 to 254. An error results if you specify a value outside this range (split: specified-value: too small or split: specified-value: too large) or you specify a non-numeric value (split: specified-value: invalid). The default is 2. If this option is specified multiple times, the last specification takes effect.
- -b num-bytes [k|m]
-
Specifies in bytes the data size for each output file. If you specify both this option and the -l option, the command displays usage information and terminates.
-
k: Specifies that the specified value is in kilobytes (1k = 1,024 bytes).
-
m: Specifies that the specified value specified is in megabytes (1m = 1,048,576 bytes).
If this option is specified multiple times, the last specification takes effect.
-
- -l num-lines
-
Specifies the number of lines for each output file. If you specify both this option and the -b option at the same time, the command displays usage information and terminates. If the -b and -l options are both omitted, the default value of 1000 (lines) is used.
- input-path-name
-
Specifies the name of the input file. If this option is omitted, the standard input is read as the input.
- prefix
-
Specifies a prefix for each file name after the split.
The file names of the output files after the split are constructed as follows:
prefix+suffix
If a prefix is specified, that character string is used. If no prefix is specified, x, y, and z are used in succession.
The suffix value is a character string consisting of lowercase alphabetic letters (a to z) of the length specified in suffix-length. Suffixes are created automatically, incrementing in alphabetical order.
For example, if the suffix length is set at two bytes, the suffix for the first file will be aa, and the successive files' suffixes will be ab, ac, ..., az, ba, bb, ....
Return codes
Return code |
Meaning |
---|---|
0 |
Normal termination |
1 or greater |
Error termination |
Notes
-
The input file will be overwritten if an output file has the same name. You can prevent this by specifying a prefix that will differentiate the output files from the input file, or else you can move the input file to a directory other than the current directory.
-
If not enough file names can be generated to accommodate the split files, the command will terminate with an error (split: too many files). However, the files that were created are not deleted. In such a case, either increase the suffix-length value or increase the value of num-bytes or num-lines.
-
If the length of the file name obtained after split processing exceeds the system's maximum value, the command outputs the following message and results in an error.
In Windows:
split : file-name : No such file or directory
In UNIX:
split : file-name : File name too long
-
In Windows, input and output are performed in the binary mode for files and for the standard input and standard output. No conversion of end-of-line codes is performed.
Usage examples
-
Split the file test1.txt into two-line segments.
$ ls test1.txt $ cat test1.txt 0001:test1.txt 0002:test1.txt 0003:test1.txt 0004:test1.txt 0005:test1.txt 0006:test1.txt 0007:test1.txt 0008:test1.txt 0009:test1.txt 0010:test1.txt $ split -l2 test1.txt $ ls test1.txt xaa xab xac xad xae $ cat xaa 0001:test1.txt 0002:test1.txt $ cat xab 0003:test1.txt 0004:test1.txt $ cat xac 0005:test1.txt 0006:test1.txt $ cat xad 0007:test1.txt 0008:test1.txt $ cat xae 0009:test1.txt 0010:test1.txt $
-
Split the file test1.txt into 40-byte segments.
$ ls test1.txt $ cat test1.txt 0001:test1.txt 0002:test1.txt 0003:test1.txt 0004:test1.txt 0005:test1.txt 0006:test1.txt 0007:test1.txt 0008:test1.txt 0009:test1.txt 0010:test1.txt $ split -a 5 -b 40 test1.txt new $ ls newaaaaa newaaaab newaaaac newaaaad test1.txt $ cat newaaaaa 0001:test1.txt 0002:test1.txt 0003:test1$
-
Display an option error message.
This message might vary depending on the platform on which the command is executed. The following shows an example in Windows:
C:\TEMP>%ADSH_OSCMD_DIR%\split -z split: illegal option -- z usage: split [-a suffix_length] [-b byte_count[k|m] | -l line_count] [file [name]]