split file into multiple files

Linux Split File into Multiple Files

Sometimes you may need to split file into multiple files or multiple parts in various ways. In this article, we will look at how to split file to make them easier to manage and transfer. We will split and csplit commands for out purpose, that allow you to easily split files into multiple parts.


Linux Split File into Multiple Files

You can split files in various ways – by size, regular expressions, patterns, or other criteria. We will look at different use cases to split files in Linux.


Split File by Size

Let us say you have a big file at /home/ubuntu/movie.iso that is 5GB in size. You can easily check its size using du command as shown below.

$ sudo du -h /home/ubuntu/movie.iso
5.0G movie.iso

Once you have clearly determined the file size, you can decide to split the file by size. For our example, we will split this file into 5 smaller files of 1G each.

Here is the command for it.

$ sudo split -b 1G /home/ubuntu/movie.iso

There are a couple of things to keep in mind while using split function. First, if you don’t specify the full path to your big file, split command will look for it in your present working directory. Secondly, you need to specify the size of smaller files after -b option. You can use G to indicate GB, M to indicate Mb and B to indicate bytes.

Once the file is split, you can list them using ls command.

$ ls /home/ubuntu
movie.iso xaa xab xac xad xae

By default, split will create files with names xaa, xab, etc, You can change this by indicating the filename prefix at the end of command. Here is an example to change the beginning of files as ‘part’.

$ sudo split -b 1G /home/ubuntu/movie.iso part-
$ ls
movie.iso part-aa part-ab part-ac part-ad part-ae

If you want to see the progress of file splitting use verbose option as shown below.

$ sudo split --verbose -b 1G /home/ubuntu/movie.iso part-
creating file part-aa
creating file part-ab
creating file part-ac
creating file part-ad
creating file part-ae


Split File by Pattern or Regular Expression

You can also split file by its content using csplit command. Let us say you have file called /home/ubuntu/story.txt which is split into chapters. So we may split the file by regular expression ‘Chapter’.

Here is the command to split the file into 5 parts based on pattern ‘Chapter’.

$ sudo csplit /home/ubuntu/story.txt /Chapter/ {5}

In the above command, you need to specify the regular expression between forward slashes and the number of splits within curly braces.

Starting from the beginning of file, cplit will split the file based on every occurrence of specified pattern. Once it has created 5 parts, it will stop splitting the file further. In other words, if your file has more than 5 chapters, it will create 1 part for each of the first 4 chapters and put all the remaining chapters in the 5th part. If you mention more number of splits than there are patterns, you will get an error saying ‘match not found…’. If you don’t mention number of splits, csplit will split the file only once, at the first occurrence of pattern.

If you don’t know the number of parts to be created, or number of times the pattern occurs, mention wildcard character asterisk ‘*’ between curly braces.

$ sudo csplit /home/ubuntu/story.txt /Chapter/ {*}

In this case, csplit will create as many parts as the number of times your pattern occurs in the file.

If you want to combine the split parts just use cat command with those filenames as shown below.

$ cat xa*
OR
$ cat xaa xab xac xad xae

If you want to combine the files into another file just redirect the output of above cat command to new file’s path.

$ cat xa* > /home/ubuntu/new_file.iso
OR
$ cat xaa xab xac xad xae > /home/ubuntu/new_file.iso

That’s it. In this article, we have looked at a couple of easy ways to split a file in Linux, and also to combine them back into a single file.

Also read:

How to Schedule Reboot in Linux
How to Redirect Nohup Output to File
How to Install Dependencies with Apt
How to Install Dpkg Dependencies Automatically
How to Disable Unnecessary Services in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *