split and join files command line linux

Sometimes you need to break large files into pieces for various reasons. Let’s say you have a large video or an archive and you need to upload this file to your Dropbox account or Google drive or even to another PC. It becomes a daunting task, especially if the upload speeds are low.

Besides consuming time and resources, it’s a bit risky in case the power goes off or if there was a disturbance in the network, rendering the broken upload useless. You have to restart all over again.

One of the best ideas to overcome this trouble is to split the large file into small segments and then upload or move them one at the time. When you want to open the file, you need to combine the file pieces and open the file.

Advantages of splitting large files into smaller ones:

  • Reduce processing time, as the smaller files are processed faster
  • Speed up data transfer through network file transfer, as smaller files are much faster than one large file.

In this tutorial, we are going to show you how to split large files into small pieces. And also how to combine those broken pieces into one file again.

Splitting Large Files Using Command Line

Now let’s try to find a large file and break it into small pieces.

Step 1. To check file size in a human-readable format, you use the following command.

du -h MovieClip.mp4

On my PC I have found a file called MovieClip.mp4 with size around 2 GB as shown in the below screenshot:

ALSO READ  How to sync date and time from the command-line in Ubuntu
MovieClip Size
MovieClip Size

As you can see this is a large file which will be hard to upload to your cloud, especially if you have a slow internet connection. So in the next step, we will show you how to split this file into smaller size files lets say 200 MB each, to make it easy while uploading.

Step 2. To split your file into smaller files with size 200 MB each and name the new data with MV, use the next command.

split -b 200M MovieClip.mp4 MV.

The previous command may take some time depending on your PC resources. So, when the command executes successfully move on to the next step.

Kindly note that you can try to execute the split command without specifying any extra arguments like next command.

split MovieClip.mp4 MV.

In this case and by default settings, the system will split the large file into small files starting with letter x and with each file containing 1000 lines.

Step 3. To check the output of the previous split command, use the ls command as follows.

ls -lh
MovieClip File and MV Files
MovieClip File and MV Files

As you can notice, you will find new files starting with MV and with size 200 MB each. Now it will be easy to upload or move or send them anywhere.

Combining Files using Command Line from Terminal

Step 1. Create a new directory to move smaller files to it.

mkdir ./NewMV/

Step 2. Move all the smaller size files starting with MV to the new directory.

mv MV* ./NewMV/

Step 3. Go to the new directory and list its content.

cd NewMV/
Create New Folder and Move Small Files To
Create New Folder and Move Small Files To

Step 4. To combine small size files into a new file called CombinedMovieClip.mp4, use the next command.

cat MV.?? > CombinedMovieClip.mp4

Also, this command may take some time depending on your PC resources. After the cat command completes, you can list the directory content to check the newly created file.

ALSO READ  Delete, Copy, Move, and Rename Files using command-line in Linux
Combine Files
Combine Files

You have successfully split your large file into small pieces and combined them again. If you need to know more about the split or cat commands you can easily refer to their manual pages using the following commands:

split manual page:

man split

cat manual page:

man cat

Finally, I hope you have enjoyed this tutorial, and for any further questions you can leave a comment, and we will be glad to help you.

Leave a Reply

avatar
  Subscribe  
Notify of