Share this post
FaceBook  Twitter  
Contents[Hide]

1. Introduction

If you have not hear about split command than you are missing a lot. As command's name suggests split command helps you to split file into smaller files. split works on any file whether it is binary or text file. This is quite useful if you cannot fit entire file on storage device such as tape or you want to split large file to be able to send it via email which has file size restrictions. You can also split large text files such as log files to smaller chunks based on the number of bytes. This article will describe syntax and usage of split command.

2. Frequently Used Options

  • -b, --bytes=SIZE
    This option specifies a size of output files
  • -d, --numeric-suffixes
    Use numeric suffixes instead of alphabetic
  • -n, --number=CHUNKS
    generate CHUNKS output files

3. Split files by output size

By default if not suffix options is used the split command will split your file into files starting with x a followed by 2 alphabetical characters. For example the first file is called xaa followed by xab, xac where last file will be called xzz. This means that your limitation in this case to to split file into maximum of 676 files. ( 26x26 ). Here is how it works. Let's say for example that we have a file of size 10MB:

$ dd if=/dev/zero of=file bs=10M count=1
1+0 records in
1+0 records out
10485760 bytes (10 MB) copied, 0.0780163 s, 134 MB/s
$ ls -lh file
-rw-rw-r-- 1 split-example split-example 10M Jan 17 15:24 file

Now we can split this file into smaller pieces of 1MB per file output by using -b option:

$ split -b 1M file
$ ls -lh
total 21M
-rw-rw-r-- 1 split-example split-example 10M Jan 17 15:24 file
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xaa
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xab
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xac
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xad
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xae
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xaf
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xag
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xah
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xai
-rw-rw-r-- 1 split-example split-example 1.0M Jan 17 15:26 xaj

4. Split by number of output files

The will be also a situation where you need exact number of output files you wish to produce. In the following split example we will split out 10MB file into max of 3 files using -n option.

$ ls -lh file
-rw-rw-r-- 1 split-example split-example 10M Jan 17 15:24 file
$ split -n 3 file
$ ls -lh *
-rw-rw-r-- 1 split-example split-example 10M Jan 17 15:24 file
-rw-rw-r-- 1 split-example split-example 3.4M Jan 17 15:33 xaa
-rw-rw-r-- 1 split-example split-example 3.4M Jan 17 15:33 xab
-rw-rw-r-- 1 split-example split-example 3.4M Jan 17 15:33 xac

5. Using numeric suffix

Split command also allows you to create files with number suffix instead of default alphabetic characters. The following command will create file for each letter in string "linuxcareer.com". The trick is that we split file based on number of bytes where 1 byte is equal to 1 character. We also instead of using alphabetic suffix will use numeric:

$ ls
$ echo linuxcareer.com | split -b 1 -d
$ ls
x00 x01 x02 x03 x04 x05 x06 x07 x08 x09 x10 x11 x12 x13 x14 x15
$ cat *
linuxcareer.com

6. Restoring the original file

Splinting files is easy by how to will restore our original file? Let's say the we have a some ISO image we would like to split into half.

$ ls -lh ubuntu-12.04.1-server-amd64.iso 
-rw-r--r-- 1 split-example split-example 658M Jan 17 15:45 ubuntu-12.04.1-server-amd64.iso

Now we split this ISO image into half with use of -n option:

$ split -n 2 ubuntu-12.04.1-server-amd64.iso
$ ls -lh x*
-rw-rw-r-- 1 split-example split-example 329M Jan 17 15:51 xaa
-rw-rw-r-- 1 split-example split-example 329M Jan 17 15:51 xab

At this point we can restore original file ubuntu-12.04.1-server-amd64.iso with use of cut command and STDOUT redirection operator.

$ cat x* > restored-ubuntu-12.04.1-server-amd64.iso

To confirm that the restored-ubuntu-12.04.1-server-amd64.iso was restored correctly and that it is the exact replica of the original ubuntu-12.04.1-server-amd64.iso we use md5sum command to create a sum for both files and then compare the output:

$ md5sum *.iso
a8c667e871f48f3a662f3fbf1c3ddb17 restored-ubuntu-12.04.1-server-amd64.iso
a8c667e871f48f3a662f3fbf1c3ddb17 ubuntu-12.04.1-server-amd64.iso

As you can see the restored file is intact as it contains same md5 digest as its original.

7. On the fly tar split

Now that we have covered the basics we can also look into more advanced example and that is splitting tar archive files on the file.

In the following example we will tar gzip /tmp/Software directory. However, instead of creating zip file we will split this archive on the fly using split command.

$ du -sh /tmp/Software 
29M /tmp/Software
$ ls
$ tar cz /tmp/Software | split -b 2M
tar: Removing leading `/' from member names
$ ls
xaa xab xac xad xae xaf xag xah xai xaj xak xal xam xan

As you ca see now we have our compressed tar archive of /tmp/Software split to multiple chunks with max size of 2MB. In the next step we will restore the original directory:

$ cat * | tar xz 
$ ls
tmp xaa xab xac xad xae xaf xag xah xai xaj xak xal xam xan
$ du -sh tmp/Software
29M tmp/Software

8. Conclusion

As you can see, when using the GNU/Linux operating system you are really limited by your imagination and skills, rather than by your wallet size. This article covered the basics of the split command. For more information and usage options use:

$ man split

Make sure you tune in to our RSS and Linux jobs portal to stay informed about the latest opportunities in the field. Also visit our Linux Forum if you want to share your Linux experiences with us or require additional help.

Partners

Who are we?

LinuxCareer.com is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.

JOIN LINUXCAREER

You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.