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 the split. and the prefix is the new chunk of files that are being created after the command.
So Let’s split and merge files in Linux.
Step 1: Let’s say you want to split our favorite grep binary into smaller chunks.
#ls -lh /bin/grep
-rwxr-xr-x. 1 root root 109K May 7 2010 /bin/grep
Step 2: Lets say the chunk size or the smaller size of spilts is 10k then use below command.
# 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:
# cd /root/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 the original file you need to do cat into with different filename.
# cat /root/test/prefix* > /root/mygrep
# ls -lh /root/mygrep
-rw-r--r--. 1 root root 109K Dec 31 19:18 /root/mygrep
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:
# md5sum /bin/grep
0462fb13c3c14409f7d3b4f80277be99 /bin/grep
# md5sum /root/mygrep
0462fb13c3c14409f7d3b4f80277be99 /root/mygrep
You will notice, both the values are the same.