Split large file and combine file in Linux

$ du -h Linux\ Security.mp4

Sample output:

1.1G Linux Security.mp4

As you see, the video file size is 1.1 GiB, which is very large to upload to my google drive. Even though, Google Drive lets users upload files up to 5TB in size, it is really time consuming process. With my low speed Internet connection, I presume it would take more than 30 minutes to upload. I don’t want to wait that much longer. So, what I am going to do is split this file into multiple smaller size files, for example 100 MB each, to make upload process faster.

Now, let us split the above file into multiple smaller files, say for example 100MB each. To do so, run:

$ split -b 100M Linux\ Security.mp4 ls.

The above command splits the Linux Security.mp4 file into 100MB chunks. This command creates files named ls.aa, ls.ab, ls.ac.. and so on.

Let us take a look at the output after splitting the large file.

$ ls

Sample output:

'Linux Security.mp4' ls.ab ls.ad ls.af ls.ah ls.aj
ls.aa ls.ac ls.ae ls.ag ls.ai ls.ak

See? Linux Security.mp4 file has been split into multiple files named ls.aa, ls.ab …. ls.ak etc. Each file size is 100MB.

Now, It is little bit easy to send them as Email attachment.

Combine files in Linux from command line

Save all files in a folder. And then, go to that folder and combine them as follows.

$ cat ls.?? > Linux_security.mp4

Here, Linux_security.mp4 is output file name. The double question marks(??) match any two-character extension in the file name. To put this simply, the filename part ls.?? matches all filenames such as ls.aa, ls.ab ..etc., and combine all of them into single file. Please be mindful that this command will combine all files that contains two-character extensions. So, be sure before combining files. If there are some other files with two letter extensions, they will also be merged into the output file.

Also, don’t forget to mention the correct extension while merging them. In case, you want to send all files via mail to your friend, tell him/her the correct extension of the files you have sent. He/she should use the same file extension in the output file while combining them.

If you don’t specify any argument in the split command, the file will split into multiple smaller files with x** as file names. Each file would contain 1000 lines by default. Here, ** is the two character suffix that is added by default with each file name.

Let us split the same file using split command without specifying any extra arguments like below.

$ split Linux\ Security.mp4

This command splits the Linux security file into multiple smaller files with x** as file names.

Run ‘ls’ command to view the files:

$ ls

Sample output:

Linux Security.mp4
xaj xat xbd xbn xbx xch xcr xdb xdl xdv xef xep xez xfj xaa xak xau xbe xbo xby xci xcs xdc xdm xdw xeg xeq xfa xfk xab xal xav xbf xbp xbz xcj xct xdd xdn xdx xeh xer xfb xfl xac xam xaw xbg xbq xca xck xcu xde xdo xdy xei xes xfc xfm xad xan xax xbh xbr xcb xcl xcv xdf xdp xdz xej xet xfd xae xao xay xbi xbs xcc xcm xcw xdg xdq xea xek xeu xfe xaf xap xaz xbj xbt xcd xcn xcx xdh xdr xeb xel xev xff xag xaq xba xbk xbu xce xco xcy xdi xds xec xem xew xfg xah xar xbb xbl xbv xcf xcp xcz xdj xdt xed xen xex xfh xai xas xbc xbm xbw xcg xcq xda xdk xdu xee xeo xey xfi

Each file should contain 1000 lines. You can also verify it using wc (word count) command as shown below.

$ wc -l *

Sample output would be:

142891 Linux Security.mp4
1000 xaa
1000 xab
1000 xac
1000 xad
1000 xae
1000 xaf
1000 xag
1000 xah
.
.
.
1000 xfl
891 xfm
285782 total

To combine all these files, run:

$ cat x* > ls.mp4

The above command will combine all files into a single file called ls.mp4.

For more details, refer man pages.

$ man split
Share

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.