A lot of time we need to carry large sized files and we do not have enough space in a single media. At that time we need to split the large file into several small ones and then carry them in separate media. Also small files are very useful in uploading a large file for sharing or backup in online storage services. Here is a very simple way how you can split a large file in several equally sized small ones in Linux/Unix and again join them in Linux/Unix or in Microsoft Windows.

Split It

Let ‘largefile.iso‘ is a 674Mega Byte sized CD image file. We will split up this file into 100Mega Byte sized images. So we will get 6 images of 100MB each and the last one consisting of 74MB. To do this launch a terminal and run the below command.

split -b100M largefile.iso largefile_part_

The above command will split the ‘largefile.iso‘ into 100MB sized parts. Each part will be name will be prefixed with largefile_part_ and a automatically generated alphabetical suffix. Below the generated files are shown through the ls command.

$ ls -lh largefile*
-rwxrwxrwx 1 Phoxis Phoxis 674M 2009-08-28 23:50 largefile.iso
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:34 largefile_part_aa
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:34 largefile_part_ab
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:34 largefile_part_ac
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:34 largefile_part_ad
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:34 largefile_part_ae
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:34 largefile_part_af
-rw-rw-r-- 1 Phoxis Phoxis  74M 2009-09-01 12:34 largefile_part_ag

The switch ‘-b‘ specifies the size of each split file. The file size should be in bytes and it may accompany a multiplier letter. In the example the value is 100 and the multiplier letter is ‘M‘ which represents Mega. Similarly you can use kB or K for Kilo , G or GB for Giga and specify the file sizes.

Note: The G and GB , kB and K, M and MB differs as follows: kB = 1000 bytes and K = 1024 bytes, MB = 1000 * 1000 bytes and M = 1024 * 1024 bytes, and GB = 1000 * 1000 * 1000 bytes and G = 1024 * 1024 * 1024 bytes.

If you do not like the alphabetical suffix, then use the ‘-d‘ switch with the split command to generate numerical suffixes. Like shown below.

split -b100M -d largefile.iso largefile_part_

This will generate numerical suffixes to the string “largefile_part_” , as shown below, with the help of the ls command.

$ ls -lh largefile*
-rwxrwxrwx 1 Phoxis Phoxis 674M 2009-08-28 23:50 largefile.iso
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:39 largefile_part_00
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:39 largefile_part_01
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:39 largefile_part_02
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:39 largefile_part_03
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:39 largefile_part_04
-rw-rw-r-- 1 Phoxis Phoxis 100M 2009-09-01 12:39 largefile_part_05
-rw-rw-r-- 1 Phoxis Phoxis  74M 2009-09-01 12:39 largefile_part_06

You can specify any suffix to the split files. In the above example the suffix was “largefile_part_” such suffixes are easy to recognize. If you do not supply any suffix then the default suffix is “x” and is used. In the case the generated files will be named x00, x01, x02 for numerical prefix. or xaa, xab, xac for alphabetical prefix etc.

Join It

Joining the files can be done in a snappy with just simply concatenating the split file contents in one single file. This can be achieved with the cat command, as shown below.

cat largefile_part_* > largefile_joined.iso

The above command will dump the “largefile_part_” suffixed files in their proper order into the file “largefile_joined.iso” . And you can then remove the “*_part_*” files. Note that the part orders are very important then joining. In case you manually supply the file names instead of using wild cards make sure you supply the file names in the proper numerical/alphabetical order.

In Microsoft Windows you need to execute the copy command with the /b switch as shown below, to join the files.

copy /b largefile_part_* largefile_joined.iso

With this handy way and the GNU coreutility tools you can make splitting files easy

Advertisement

2 thoughts on “Split and Join Files

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s