X
    Categories: Linux

Split and merge files in linux.

We can split and merge files in linux especially for large once with the split command, and to join you just need to cat the files into one file. Split can be used on binaries as well on text files.

split [options] filename prefix

Here filename is the file that you want split. and prefix is the new chunk of files that’s being created after command.

So Let’s split and merge files in linux.

Step 1: Let’s say you want to split our favourite grep binary into smaller chunks.

[root@rhel1 ~]# ls -lh /bin/grep
-rwxr-xr-x. 1 root root 109K May 7 2010 /bin/grep
[root@rhel1 ~]#

Step 2: Lets say the chunk size or the smaller size of spilts is 10k then use below command.

[root@rhel1 bin]# split --byte=10k /bin/grep /root/test/prefix

It will split grep binary into 10kb files under /root/test directory and all filenames will be prefixaa prefixab etc. as below:

[root@rhel1 bin]# cd /root/test/
[root@rhel1 test]# ll
total 132
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixaa
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixab
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixac
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixad
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixae
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixaf
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixag
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixah
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixai
-rw-r--r--. 1 root root 10240 Dec 31 19:18 prefixaj
-rw-r--r--. 1 root root 9080 Dec 31 19:18 prefixak

Step 3: Now to retrieve original file you need to do cat into with different filename.

[root@rhel1 test]# cat /root/test/prefix* > /root/mygrep
[root@rhel1 ~]# ls -lh /root/mygrep
-rw-r--r--. 1 root root 109K Dec 31 19:18 /root/mygrep
[root@rhel1 ~]#

It creates a binary file under /root with name mygrep.

Step 4: To check the integrity of both the files we can use md5sum as below:

[root@rhel1 test]# md5sum /bin/grep
0462fb13c3c14409f7d3b4f80277be99 /bin/grep
[root@rhel1 test]# md5sum /root/mygrep
0462fb13c3c14409f7d3b4f80277be99 /root/mygrep

You will notice, both the values are the same.

Related Post