split Command in Linux | Explained

Linux is the most reliable OS in terms of security. Due to its strong security mechanism, it is preferred over any OS for sending files/data to the destination. However, sending a large file creates an issue sometimes and it will not be sent due to its large size. That is where the split command of Linux comes into action and helps deal with such kinds of scenarios.

Today, the tutorial is about the split command in Linux. 

Content Covered

  1. What is a split Command in Linux?
  2. How Does split Command Work?
  3. How to Use Split Command in Linux?
  4. Example 1: Split File’s Content
  5. Example 2: Split File’s Content in a Verbose Mode
  6. Example 3: Specify the Number of Lines
  7. Example 4: Split File Content For a Specific File Size
  8. Example 5: Limit the Max File Size For Files
  9. Example 6: Limit Number of Files For Splitted Content
  10. Example 7: Split Content with Complete End of Line
  11. Example 8: Display Specific Files Before Splitting
  12. Example 9: Define Suffix Length
  13. Example 10: Define Suffix Name For Splitted Content
  14. Example 11: Change Prefix For the Splitted File
  15. Example 12: Delete Files Having No Size
  16. Example 13: Split File Content and Zip
  17. Bonus Tip: How to Rejoin the Splitted File?
  18. Wrap-Up 

What is a split Command in Linux?

The split is the command line utility of Linux that allows users to divide large files into pieces of small size multiple based on the number of lines. The default behaviour of the split command is to split the files with 1000 lines in each file. Moreover, this command has a vast range of options to split a file based on different factors. 

How Does split Command Work?

The working of the split command depends upon various factors such as file sizes, max files to split, content lines to split, and more. For this purpose, the split command comes with various flags/options. 

Different Flags/Options of split Command

Here are the flags/options that the split command offers. 

Flag/OptionsDescription
-b, –bytes=SIZETo define the file size in bytes
-n, –number=CHUNKSTo create a particular number of files
-v, –verboseEnables the verbose mode of the split command 
-[no of lines]Simply putting the hyphen (-) with numbers defines the number of lines in each file.
-CTo define maximum file size.
-eStops to generate empty output files
-dTo define the suffix in numeric format.

For more options/flags, execute the “–help” command:

split --help

Syntax

The split command of Linux uses the following syntax:

split [-Options] [File Name] [Prefix]

The description of the above syntax is given below:

  • Options: Specify the suitable option with a split command
  • File Name: Mention the file name to split
  • Prefix: Define the prefix for the files if needed.

How to Use split Command in Linux?

To use the split command in Linux, let’s practice some of the practical examples below:

Example 1: Split File’s Content

To split the large file, use the “split” command and specify the file name. For instance, the command will split the “test.txt” file into small pieces of files, and the “ls” command is for checking the results:

split test.txtls

The test file is split into multiple files starting with the name “xa”.

As explained earlier, the default mechanism of the split command is to generate each small file with 1000 lines. If the remaining lines are not equal to 1000, the last file will contain the remaining lines as it is. To verify this statement, let’s check the lines in “test.txt” and all split files. For this purpose, use the “wc” command with the “-l” flag and give the file names:

wc -l test.txt xa*

In our case, the file “test.txt” has a total line of 7493 and each split file contains 1000 lines except the last one. 

Example 2: Split File’s Content in a Verbose Mode

The normal execution of the split command gives no detail for creating files, but the verbose mode can be enabled to do so. For this, use the “–verbose” option/flag in the command:

split --verbose test.txt

The split command is executed in the verbose mode. 

Example 3: Specify the Number of Lines

To discard the default behaviour of the split command for splitting files with 1000 lines, specify the number of lines with the “hyphen” (-) sign. As proof, the following command splits the “test.txt” file with 500 lines in each file:

split -500 test.txt
ls

The file “test.txt” is split.

Verify the number of lines within each file with the “wc -l” command:

wc -l xa*

The split files have 500 lines.

Example 4: Split File Content For a Specific File Size

Split command offers the feature of creating specific file sizes using the “-b” flag. Based on the sizes such as bytes, kilobytes, megabytes, and gigabytes, the options are:

  • “-bn”, for categorizing the size in no of bytes (n).
  • “-bnk” for categorizing the size in terms of kilobytes (n).
  • “-bnM” for categorizing the size in terms of megabytes (n).
  • “-bnG” for categorizing the size in terms of kilobytes (n).

To illustrate, the command will split files with the size of 346 kilobytes:

split -b346k test.txt

The file “test.txt” is split with the defined file size.

For the verification of the file size, use the “-c” flag of the “wc” command that displays the number of bytes:

wc -c xa*

It can be verified that the generated files have the defined sizes.

Example 5: Limit the Max File Size For Files

To specify the maximum file size for the split content, use the “-C” flag and define the maximum file size. In the following command, the defined maximum file size is 1MB:

split test.txt -C 1MB
ls

The “test.txt” file is split with a maximum file size of 1 MB.

To check the sizes, run the “wc” command with the “-c” flag:

wc -c test.txt xa*

Here, the displayed size is in the bytes which are equal to 1MB. 

Example 6: Limit Number of Files For Splitted Content

Likewise, to limit the specific number of files to split, use the “-n” flag and give the required number:

split test.txt -n 5
ls

The file “test.txt” is split into the 5 small files as specified.

Example 7: Split Content with Complete End of Line

To split file content with a complete end-of-line in each file, utilize both the “n” and “l” flags. With the “l” flag define the number of files to be created with the help of “/”. 

In the below scenario, the command will create 10 files with a complete end of the line: 

split -n l/10 test.txt

The file “test.txt” has been split.

Now, display any of the generated file content with the help of the “cat” command for the verification:

cat xaa

It can be seen that file content ends with the complete end of the line.

Example 8: Display Specific Files Before Splitting

Interestingly, you can also use the split command to analyze results before splitting. To do this, define the specific file in the format “specific file/total files”. For instance, the command divides the 2800 files but displays the output of the first one:

split -n 1/2800 test.txt

The output of the specific output file (first) is printed. 

Example 9: Define Suffix Length

The split command displays the suffix length of 3 for the file name by default. The user can change and define it using the “-a” flag. Let’s say the desired suffix length is 4 and the command will look like this:

split -a 4 test.txt
ls

The files are generated with the suffix length of 4.

Example 10: Define Suffix Name For Splitted Content

It can be noticed that the suffix name for the split files is in the alphabetic format. To transform this suffix format into the numeric, put the “-d” flag in the command:

split -d test.txt
ls

The suffix format is transformed into a numeric one.

Example 11: Change Prefix For the Splitted File

The split command has the default prefix of “xa” for the files. To change this prefix with the desired one, use the “-d” flag and specify the prefix at the end of the command:

split -d test.txt He
ls

The files are split with the prefix “He” as given.

Example 12: Delete Files Having No Size

While splitting large files, the files having 0 size may be generated. To omit files having no size while splitting, utilize the “-e” flag in the command:

split -n10 -e test.txt
ls

Upon using the “-e” flag, the 0 sizes will be omitted if there are any. 

Example 13: Split File Content and Zip

To split files and merge them into one zip file, forward the split command output to the gzip command and define the zip file name. For the illustration, see the below command execution:

split -n12 test.txt | gzip > allfiles.zip
ls

The file’s content is split and merged into the “allfiles.zip” file.

Bonus Tip: How to Rejoin the Splitted File?

The user can rejoin all the splitted files by using the cat command as the split command doesn’t offer this functionality. To rejoin all files, use the cat command, specify file names, and forward all output to the one file using the redirection operator. Here, “*” is used for all split files starting with “xa”:

cat xa* > onefile.txt
ls

All split files are merged into a “onefile.txt” file. 

Wrap-Up

The split is a Linux command line utility of Linux considered for breaking down large files into multiple small files. Additionally, this command comes with different options/flags (such as “-b”, “-n”, and “-C”) and is used according to the user’s requirements.

This handy guide has taught detailed knowledge of split commands. Besides this, the method to rejoin the split files has also been discussed in this post.