How To Split A Large File Into Multiple Smaller Files In Linux/Unix?


Suppose you have an archive file of size more than 5GB, and if you want to burn it on a disc or transfer it to a filesystem with limited max filesize (say FAT32 with a limit of 4GB per file) then how will you do that. You have to split this file. Linux or Unix provides split command to break a file into multiple pieces. In this article, we will discuss split command with some examples.

Syntax of the split command

The syntax of the split command is given below –

split [options] filename prefix

Where replace the filename with the large file that you wish to split. Replace the prefix with the name you wish to give small output files.

In options, you could use –

OptionsDescription
-b, --bytes=NPut N byte per output file
-l, --lines= NPut N lines per record output files
-n, --number= NIt will generate N number of output files
--helpDisplay the help and exit
--versionThis will print the version split program and exit

There are a lot more options available you can check it in its manual page. Use the following command to open man page –

man split

How to split a large file into smaller ones?

To split a large file into smaller pieces you will have to use the following commands in your terminal-

Suppose we have named large_file with 3000 lines long. You can simply use –

split large_file

By default, split puts 1000 lines of input or whatever is left over for the last section, into each output file. This will output three 1000 line files xaa, xab and xac.

If you want to split the same file into 500 lines long parts then use-

split -l 500 large_file part

This will output six 500 lines long files with alphabets in the suffix: partaa, partab, partac, partad, partae and partaf.

Splitting a large archive file –

Recently I backed up my system, the archive backup.tar.gz created is of the size 21.3GB. Now to split it into files with the size less than 4GB(say 3900MB) use the following command –

split -d -b 3900m /backup.tar.gz backup

This will create backup00, backup01, backup02, backup03, backup04, and backup05. Here -d is used for digits in suffix instead of using alphabets and -b is used for bytes.

You can see the split files along with some other information such as size, date of creation, etc by running the following command –

ls -lh

Conclusion

Here we discussed to split a large file into multiple smaller pieces. You may also want to know how to reconstitute a file with cat command in Linux/Unix.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.