Split a file into two or more files
The split command splits a file into two or more files. You need to specify two parameters:
- the prefix of the output file, to which an alphabetic code (aa, ab…) is added.
- the size of the individual files.
You can specify the size of the output files in two ways:
- by bytes – the -b option breaks the file into pieces of the size defined in bytes.
- by lines – the -l option breaks the file into pieces with that many lines.
Consider this example. We have created a file called file.txt:
We can split that file by bytes:
In the example above you can see that we have splitted the file file.txt by bytes (55 in this case). The prefix specified was new_file_. You can see that the split command created two files, new_file_aa and new_file_ab.
To split by lines, use the -l option:
In the example above we have first numbered the lines using the nl command. We’ve then splitted the file into pieces with one line each.