Splitting files into pieces and rejoining them

Posted by Steve on Thu 4 Aug 2005 at 14:47

There are times when large files need to be chopped into small pieces, for easier movement, or for transfer via email with size limits in place. With the use of standard shell commands you can accomplish this easily.

The most natural way to do it is to use the aptly named split command which is contained in the coreutils package - and installed upon all Debian systems.

As its name suggests split will allow you to convert one large file into multiple smaller ones. To control it you simply specify the maximum size of each output piece.

Assuming you have a 2Mb AVI file which you need to email to somebody who cannot accept files larger than 1Mb you could run:

 split --bytes=1000000 1.avi output

This will split the input file, 1.avi, into separate files each containing not more than 1000000 bytes. The files will be named output??.

Once this has been done you'll two files, outputaa, and outputab.

If you wish to use numbers instead of letters in the output filename you can use the -d falg instead. (Or the longer version --numeric-suffixes. Debian's bash package with tab completion makes the longer form easier to use for me.):

split --numeric-suffixes --bytes=1000000 1.avi output

This will result in the files output00, output01, etc.

Joining files

Rejoining files is the very simple using the cat command. Simply invoke it with a list of all the files you wish to be joined - and redirect it to your desired output file.

Assuming you wish to place the contents of the files split1, split2, and split3 into the file output you could run:

cat split1 split2 split3 > output

Or if you have no other files beginning with split you can take advantage of your shells alphabetical ordering when performing glob operations to run:

cat split* > output

This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2005 Steve - please ask for permission to republish or translate.