Home Linux Tips & Tricks How To Split And Combine Files From Command Line In Linux

How To Split And Combine Files From Command Line In Linux

By sk
Published: Updated: 57.5K views

In this brief tutorial, I will show how to split and combine files from command line in Linux and Unix-like operating systems using "split" and "cat" commands.

Introduction

I have a lot of Linux tutorial videos in my Linux desktop and I wanted to move all of them to my Google Drive. The problem is the size of some video files is more than 1 GiB. 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 thought 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. You might have different reasons 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. You can join all of them later to make a single file.

Split And Combine Files From Command Line In Linux

First, let us see how to split a big file into multiple smaller files.

Split files in Linux from command line

Have a look at the size of the following video file.

$ 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

Recommend Read:


Conclusion

There could be other CLI and GUI tools available in Linux to split or combine files. But, 'split' is built-in command that comes pre-installed. So, don't bother installing any additional tools on your Linux box. Also, split command breaks the file into multiple pieces quickly.

You May Also Like

1 comment

Anonymous May 1, 2020 - 12:02 am

Thanks, now I can install Destiny on to my PS3! 😀

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More