Files can grow to a large size over time & these large files will not be easy to read or edit. Or if we need to move them to other systems, they won’t fit into the USB drive. For such scenarios, Linux presents us with a very useful command, SPLIT command.
As the name would suggest, the split command helps us to split the files into smaller files as per our requirements. We split the files by number lines, size, length etc. In this tutorial, we will discuss the use of split command with some examples.
Recommended Read: Linux commands you should never run on your system
Also Read: Learn to use CURL command with examples
Split file based on number of lines
By default, the split command splits a file with 1000 lines in each split file. So if we have a file with 2300 lines, we will get three files with 1000, 1000 & 300 lines. The command for that is,
# split files.txt
In the output, we will get files named ‘xaa’, ‘xab’ & ‘xac’ along with the original file.
Now if we need to change the default behavior i.e. change the number of lines per file, then we will use ‘l’ option,
# split -l 500 files.txt
This is will produce files with 500 lines each only.
Split files with a given prefix
To change the file names from ‘xaa’, ‘xab’ & so on to another prefix, we can use the following command,
# split files.txt new_prefix.txt
Now the files created would have the following names new_prefix.txtaa, new_prefix.txtab etc. We can also choose to add numbers to prefix rather alphabets, by using the option ‘d’,
# split -d files.txt. New_prefix.txt
Now output files would be named ‘new_prefix.txt01’, ‘new_prefix.txt02’.
Split files based on the file size
To split a large file into smaller files based on the size and not lines, we can use the following command,
# split -b 100M files.txt
So, here we are splitting the file to smaller files, all with size 100 Mb.
That’s it guys, we now end this tutorial on split command. Please do let us know if you have any questions, queries or suggestions using the comment box below.