How To Split And Combine Files From Command Line In Linux
I have a lot of Linux tutorial videos in my Linux desktop. I wanted to move all of them to my Google Drive. The problem is that the video files are more than 1 GiB in size. It is practically not a good approach to upload such big files to the Google drive. Even though Google Drive allows us to upload files up to 5TB, I find it is very time consuming process to upload all big files. While contemplating with this issue, I figured it out that it would be much better if I can be able to split those files into smaller size and upload them one by one. I can, then, download all parts of the file, and combine all of them, whenever I want. This is just an example. There might be many chances that you want to break a large file into multiple pieces and combine them later. If you ever been in this situation, afraid not. There is a simple command line utility called “split” which is used to split the big files into multiple smaller files, and you can join all of them late to make a single file. Sounds useful? Indeed.
In this brief tutorial, I will show how to split and combine files from command line in Unix-like operating systems. Read on.
Split And Combine Files From Command Line In Linux
First, let us split a big file.
Split files in Linux from command line
Have a look at the size of the following video file.
du -h Linux\ Security.mp4
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 break up 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.
'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:
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.
- How to split or extract particular pages from PDF file
- How To Merge PDF Files In Command Line On Linux
Of course, there are many other CLI and GUI tools available in Linux to split or combine files. But, ‘split’ is built-in command that comes with Linux Kernel. So, don’t bother installing any additional tools on your Linux box. Also, split command breaks the file into multiple pieces quickly.
And, that’s all. If you know any other methods to do this task, feel free to let me know in the comment section below. I am all ears!
If you find our guides useful, please share them on your social and professional networks, so that others also can benefit from them. I will be soon here with another useful guide. Till then, stay tuned with OSTechNix!
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!