Linux offers the “split” command line utility to split a large file into various parts. This command line tool is present in almost all the commonly used Linux distributions like RedHat, CentOS, Fedora, Ubuntu, and many others. Its main purpose is to break the large “log” and “archive” files into smaller files as they contain a large amount of data
This guide illustrates the procedure to split a file into different parts either by size or by content. The outcomes of this guide are as follows:
- How do I split a file into parts in Linux?
- Split a File into Parts by Size
- Split a File into Parts by Lines
- Bonus Tip: How to Join the Split Parts of a File??
Let’s start with the splitting of a specified file in Linux.
How do I Split a File into Parts in Linux?
In the “split” command, the names of the files are PREFIXaa, PREFIXab, PREFIXac, and so on. By default, each splitter file is divided into “1000” lines with the file prefixes name “x”. The user can change or modify the name and number of lines per requirements.
To perform this task, the “split” command offers the “-b” (for size in bytes) and “-l” (for no of lines) arguments. In this section, the practical implementation of both arguments is described with the help of examples.
Example 1: Split a File into Parts by Size
The “notes” directory contains a file “Sample.txt” of size “116k” as shown in the screenshot:
$ ls -lh
The “-b” option specifies the size of each chunk, in bytes. In the example, the “29k” option tells the split to create chunks with a size of “29KB” each. Whereas “Sample.txt” is the name of the input file that you want to split as shown:
$ split -b 29k Sample.txt SPart
So, the command will split the file Sample.txt into chunks of 29KB each, and name the output files using the prefix SPart. For example, the first chunk will be named “SPartaa”, the second chunk will be named “SPartab”, and so on.
Example 2: Split a File into Parts by Lines
Apart from the size the “split” command is also beneficial to set the no of lines in each of the splitter parts. As the “Extra” directory contains a file “NewFile1.txt” of size “5.7k”:
$ ls -lh
The “-l” option specifies the number of lines that each chunk should contain. In the example, the “12 ” tells the split to create chunks with 12 lines each. Whereas the “NewFile1.txt” is the name of the input file that you want to split.
$ split -l 12 NewFile1.txt FNew
So, the command you provided will split the file NewFile1.txt into chunks of 12 lines each, and name the output files using the prefix FNew. For example, the first chunk will be named “FNewaa”; the second chunk will be named “FNewab”, and so on.
For the verification, use the “cat” command to check the no of lines of the specified splitter files. In this case, the “FNewah” files content is checked:
$ cat FNewah
The output displays that the “FNewah” file contains “12” lines.
Bonus Tip: How to Join the Split Parts of a File?
Once the files are split into parts, they can be merged using the “cat” Linux command. Suppose to join the splitting parts of a “NewFile1.txt” file, use the cat command with the wild card “*” in the following way:
$ cat FNew* > joinedfile
As all the split files of “NewFile1.txt” have been joined and saved into the “joinedfile”. It is verified by again running the “cat” command with “joinedfile”.
That’s all about splitting a file into parts.
Linux provides a “split” command line tool to split a file into parts. It offers the “-b” and “-l” flags to split a file into parts according to size and no of lines. The “split” command is generally beneficial to split large files. This guide has provided a complete procedure to split files into parts in Linux with the help of practical examples.