The Linux Split Command [13 Practical Examples]

Try this guide with our instant dedicated server for as low as 40 Euros

linux split command

Have you ever come across large files that are difficult to deal with?  If so, you will appreciate the utility of the Linux split command.

The Linux split command is a handy tool that divides large files into smaller, easier-to-handle pieces. This is particularly useful when you have huge text files that you need to parse through or upload to other locations. The command makes this possible by splitting the file into several smaller files. 

By default, the split command divides these files into parts containing 1000 lines. However, users can change this limit to meet their needs.

In this detailed tutorial, we will take a close look at the Linux split command. We will present 13 examples to demonstrate its utility.

Let’s start with a short overview of the Linux split syntax.

Table Of Contents

  1. The Linux Split Command Syntax
  2. Linux Split Command Options
  3. The Linux split Command Examples
    1. The Prerequisite
    2. Example #1: Split Into Multiple Files
    3. Example #2: Use the Verbose Option
    4. Example #3: Set the Number of Lines per File
    5. Example #4: Choose the Fragment File Size Parameter
    6. Example #5: Specify Maximum Fragment File Size
    7. Example #6: Set the Number of Output Files
    8. Example #7: Split a File at the End of a Complete Line
    9. Example #8: Display Only a Specified Output File
    10. Example #9: Set the Suffix Length
    11. Example #10: Change the Suffix
    12. Example #11: Change the Fragment Name Prefix
    13. Example #12: Omit Files with Zero Size
    14. Example #13: Reconnect Split Files
  4. Conclusion
  5. FAQs

The Linux Split Command Syntax

The basic split command syntax is as follows:

# split [options] [file] [prefix]

Here,

[options] (optional) are special instructions for the command. 

Replace [file] with the file you want to split.

[prefix] (an addition to the start of the names of the split files) is optional.

If you don’t specify a prefix, the command will automatically use x as the prefix. Therefore, the resulting names will be in the format xaa, xab, xac, and so on.

Linux Split Command Options

The split command has several options to customize how it divides a file. Here are some of the most commonly used options:

Linux Split Command Options

The Linux split Command Examples

The split command helps you break down and manage large files on Linux systems. The following 13 practical examples will help you understand how to use the split command effectively to manage large files according to your specific needs.

The Prerequisite

Before understanding how to use the split command, ensure you have the following:

  • Terminal access 
  • A large text file to experiment upon 

Example #1: Split Into Multiple Files

A common, or perhaps the default use of the split command is to break a large file into smaller pieces, each containing 1000 lines. 

For instance, run the following command to split a file named large_text_file into smaller chunks:

# split large_text_file

Once you split the file, verify the command’s output by this command:

# ls

split large_text_file

The output shows that the command divided the original file into 15 new files with names ranging from xaa to xap.

We also recommend determining the number of lines in each file with the wc command with the -l option:

# wc -l large_text_file xa*

wc -l large_text_file xa

This command displays the line count in the original large_text_file and all the split files starting with xa.

For instance, here, the file large_text_file has 15,080 lines. When you use the split command, it creates 15 new files, each with 1,000 lines. If the total number of lines in large_text_file weren’t a perfect multiple of 1,000, each split file would still have 1,000 lines, but the final file would have fewer than 1,000 lines. This happens because the split command divides the file evenly, and the remaining lines ( less than 1000 in number) are moved to the last file.

In contrast, consider another file named smaller_text_file with 11,310 lines in a folder small_directory. We will now split smaller_text_file into smaller parts and use ls to confirm the outcome.

# split smaller_text_file

Now, verify the outcome:

# ls

split smaller_text_file

Once the target file is split, run wc -l again to count the lines in each file:

# wc -l smaller_text_file xa*

wc -l smaller_text_file xa

The output displays that the original file is split into 12 smaller files. The first 11 files have 1000 lines each, and the last one with 310 lines.

Example #2: Use the Verbose Option

By default, the split command does not print any output. 

We recommend the –verbose option to track details on how the split command works. This allows you to track the process of the command and thereby verify and troubleshoot issues.

For instance, consider the following command that tracks how split works on the large_text_file file:

# split large_text_file --verbose

split large_text_file --verbose

You can see that the command displays more details about the process with messages, such as creating file xaa, xab, and more for each split file created.

Example #3: Set the Number of Lines per File

The split command offers flexibility when it comes to file size. If you prefer to change the default setting of 1000 lines per file fragment, use the -l option with the split command to specify a different number of lines.

For instance, to create fragment files, each containing 2500 lines, run the command:

# split -l2500 large_text_file

Once the file is split, verify the line count in each file with the wc command with the -l option. The syntax of this command will be as follows:

# wc -l large_text_file xa*

split -l2500 large_text_file

Here, the command generates six new files. The first five files are named from xaa to xaf, each containing 2500 lines. The sixth file, xag, would have only 500 lines.

The split -l command can also be used to create files with less than 1000 lines each.

For instance, consider a file named tiny_text_file that contains 3770 lines:

We can split the file into 500-line fragments with the following command:

# split -l500 tiny_text_file

split -l500 tiny_text_file

The command splits tiny_text_file into five 500-line files and one 270-line file.

Example #4: Choose the Fragment File Size Parameter

While split usually works with line count, it is not just limited to it. Instead, you can split files based on other parameters using the split -b command. This command allows you to specify the size of each file.

The syntax is split -b followed by the size and its unit:

  • Bytes with split -bn (where n is the number of bytes),
  • Kilobytes with split -bnK (where n is the number of kilobytes),
  • Megabytes with split -bnM (where n is the number of megabytes),
  • Gigabytes with split -bnG (where n is the number of gigabytes).

For instance, to create files of 1500 kilobytes each from a file large_text_file, run the following command:

# split -b1500K large_text_file --verbose

split -b1500K large_text_file --verbose

Here,

  • split -b allows you to split files based on size.
  • K specifies the size units in kilobytes.
  • The –verbose option reveals that files were created with split -bnK.

Next, verify the size of each file by using the wc -c command:

# wc -c large_text_file xa*

wc -c large_text_file xa

Example #5: Specify Maximum Fragment File Size

While the split command typically aims for even distribution during division, the -C option lets you set a maximum size for each split file. 

For instance, if you want to split a file large_text_file into parts with a maximum size of 2 MB per fragment file, run this command:

# split large_text_file -C 2MB

split large_text_file -C 2MB

To verify the output, use the wc -c command:

# wc -c large_text_file x*

Here, the split files are (ideally) under the specified maximum size of 2 MB, while the last file is smaller as the total file size isn’t a perfect multiple of the limit.

Example #6: Set the Number of Output Files

While the split command normally creates files based on line count or size until the end of the original file, the -n option allows you to specify a desired number of output files.

For instance, the following command divides a file large_text_file into ten equal parts:

# split large_text_file -n 10

split large_text_file -n 10

Note: If the original file size isn’t perfectly divisible by the number, the last file(s) might be empty.

Example #7: Split a File at the End of a Complete Line

Another use of the split command is to divide a file into parts that end on complete lines. 

You can use the -n l option with the split command for this purpose. 

For instance, to split a file large_text_file into ten parts, each ending at the end of a line, run the following command:

# split -n l/10 large_text_file

split -n l10 large_text_file

Once you split the files, use the ls command to view the newly created files. 

Next, verify each file ends on a complete line by printing the contents of a file with the cat command in the terminal.

# cat xaa

cat xaa

Example #8: Display Only a Specified Output File

In general, the split command produces as many files as needed to split the entire source file into 1000-line fragments. 

The -n option allows you to split the file and only display a specific part directly in the terminal, without creating any output files.

For instance, consider the following command that divides a file tiny_text_file into 100 parts and only displays the first part in the terminal: 

# split -n 1/100 tiny_text_file

split -n 1100 tiny_text_file

The command prints the first split file to the standard output without creating any new files.

Example #9: Set the Suffix Length

By default, the split command appends a two-letter suffix to the split files. The -a option allows you to adjust this length.

For instance, if you want to create files with a three-character suffix from a file large_text_file, run this command:

# split -a 3 large_text_file

split -a 3 large_text_file

The command outputs split files named aaa, aab, and aac (all three characters long).

Example #10: Change the Suffix

The split command offers flexibility not only in the number of lines but also in the type of suffixes used for the split files. For instance, you can use a numeric suffix for the fragments. 

The -d option instructs split to use numeric suffixes (starting from 0) instead of the default alphabetic ones (xaa, xab).

For instance, run the following command to split a file large_text_file into parts with 2500 lines per file and numeric suffixes:

# split -l2500 -d large_text_file

split -l2500 -d large_text_file

The output shows files with numeric suffixes. Using the -l2500 flag, the file large_text_file is divided into six parts, with each part containing 2500 lines.

Example #11: Change the Fragment Name Prefix

While split uses x as the default prefix for the names of the fragments, you can use the prefix of your choice. The general format for specifying the prefix in a split command is as follows:

# split [file] [prefix]

For instance, if you want to split a file large_text_file into ten files with prefix part (the fragment will be named part00 to part09), run:

# split -d large_text_file part -n 10

split -d large_text_file part -n 10

Here, -d creates files with the prefix part and numeric suffix, while the -n flag divides the file into 10 equal parts.

Example #12: Omit Files with Zero Size

When using the split command, it’s possible to end up with output files that are zero in  size. 

To avoid creating these zero-size files, you can use the -e flag with the command. 

For instance, if you want to split a file named xaa from the tiny_directory into 15 files with numeric suffixes and ensure that no zero-size files are created as the result of  the command output, run this command in the terminal:

# split -n15 -e -d xaa

split -n15 -e -d xaa

This command splits xaa into 15 parts and skips creating any empty files.

Next, check file size with the wc -c command:

# wc -c x0* x1*

wc -c x0 x1

The x0* and x1* ensure wc -c prints the size of all files in the directory, starting with numbers.

Example #13: Reconnect Split Files

Although the split command itself cannot rejoin files, you can use the Linux cat command to merge the fragments. 

The cat command not only displays file contents but can also be used to merge the smaller files into one complete file.

For instance, consider the scenario where the large_text_file file is split into ten smaller files.

ls

The names of all the output files start with x.

Apply cat to any items starting with x to merge them. However, cat prints the result to the standard output. To merge the files into a new file, use redirection (>) with the new file name.

For instance, to merge all files starting with x into a new file new_large_text, use the following command:

# cat x* > new_large_text

cat x* > new_large_text

Running wc -c shows that large_text_file and new_large_text are the same size.

Conclusion

The Linux split command is an indispensable tool for managing large files effectively. Whether you’re working with textual data or binary data, understanding how to leverage split can significantly streamline your workflow.

If you’re looking to implement robust data handling and hosting solutions, consider RedSwitches Bare Metal Hosting, which offers high-performance solutions for demanding business applications.

FAQs

Q. What is the split command in Linux?

The split command in Linux is a command-line utility that allows you to split a file into smaller parts based on specific criteria, such as the number of lines or the size of the resulting files.

Q. How can I split a large file using the split command?

To split a large file using the split command, you can use the syntax split -b followed by the desired file size or split -l followed by the number of lines each resulting file should have. For example, split -b 1000m largefile.txt will split the file into 1000MB parts.

Q. Can the split command be used to split a file into multiple files?

Yes, the split command in Linux can be used to split a file into multiple smaller files based on the specified criteria. You can use options like -d to add numeric suffixes to the resulting files.

Q. What is a file of size, and how is it relevant to using the split command?

A file of size specifies the exact size each output file should have when using the split command. For example, splitting a large file into multiple smaller files of specific sizes, such as in byte pieces or kilobytes.

Q. How can I use a FILE PATTERN with the split command?

Using a FILE PATTERN, especially when working with the file prefix, allows you to control the naming of output files, making them easier to identify and manage in file storage systems.

Q. What does file prefix mean in the context of splitting files?

A file prefix is a text string that appears before the suffix in the names of output files generated by the split command. It helps in organizing and identifying the files more easily.

Q. Why is file storage important when using the split command?

Proper file storage considerations are crucial when splitting large files to ensure that the output files are stored efficiently without taking up unnecessary space or causing disorganization.

Q. How can I verify the integrity of a split file with the md5sum utility?

After splitting a file, you can use the md5sum utility to generate and compare the MD5 hashes of the original and split files. This guarantees that the data integrity of the split files remains consistent with the original file.

Q. What happens to a file by default when I use the split command without any options?

If no specific size or line count options are provided, the split command automatically breaks a file into segments of 1000 lines each by default.

Q. Can I create a split file with a customized suffix? How?

Yes, you can customize the suffix of split files using the –additional-suffix option followed by the desired suffix. This allows for better customization and organization of the output files.

Q. What are byte pieces, and how are they relevant to splitting files?

Byte pieces refer to splitting files into parts measured in bytes. This is useful for precise control over file size, particularly in scenarios where exact data size matters.

Q. How can I check the size of files in bytes after splitting them with the split command?

You can specify the size of the output files in bytes using the -b option followed by the number of bytes, allowing for precise control over the size of each split part.

Q. Is there an alternative option to using cat for merging files split by the split command?

While cat is the most common method for merging files, alternative methods like using tar or zip utilities can also reassemble files, providing additional features like compression.

 

Try this guide with our instant dedicated server for as low as 40 Euros