Linux Command split and 4 Options

The Linux command split is used when you need to split a large file into several smaller ones. This is particularly helpful when dealing with system log files or large data files, as files that are too large can be difficult to manage or may not be processed properly by certain programs. In this post, we will cover the basic usage of the split command, key options, and some useful tips for its application.

What is the Linux Command split?

The split command splits a file into several smaller files according to a specified size or number of lines. This utility is included by default in Linux systems and is very useful for managing or transferring large files.

Basic Usage

The basic syntax of the split command is as follows:

split [options] filename [prefix]
ShellScript

For example, if you want to split a text file called sample.txt into smaller files, you would enter the following:

split sample.txt
ShellScript

This command will split the file into several smaller files, each containing 1,000 lines by default. The filenames will be automatically generated in the format xaa, xab, xac, and so on.

Figure 1. Linux command split: Default behavior splits files into chunks of 1,000 lines
Figure 1. Linux command split: Default behavior splits files into chunks of 1,000 lines

Key Options

The split command offers several options that allow you to customize how the file is split and how the output files are named. Let’s take a look at some of the key options.

Split by Number of Lines (-l option)

By default, split breaks the file into chunks of 1,000 lines. If you want to specify a different number of lines, you can use the -l option.

split -l 500 sample.txt
ShellScript

This command will split the sample.txt file into smaller files, each containing 500 lines.

Figure 2. Linux command split: Using the -l option to split by number of lines
Figure 2. Linux command split: Using the -l option to split by number of lines

Split by File Size (-b option)

If you want to split a file by size rather than by number of lines, you can use the -b option. You can specify sizes using units like K (kilobytes), M (megabytes), or G (gigabytes). If no unit is specified, the size is measured in bytes.

split -b 30K sample.txt
ShellScript

This command splits sample.txt into files that are 30KB each.

Figure 3. Linux command split: Using the -b option to split by file size
Figure 3. Linux command split: Using the -b option to split by file size

Set a Prefix for Output Files (-d option)

If you want to name the output files with a specific prefix rather than the default alphabetic suffix, you can use the -d option.

split -d -b 50K sample.txt part_
ShellScript

This command splits sample.txt into 50KB chunks and names the files part_00, part_01, part_02, and so on.

Figure 4. Linux command split: Using the -d option to set a prefix for output files
Figure 4. Linux command split: Using the -d option to set a prefix for output files

Merging Split Files

Once you’ve split a file using the split command, you can easily merge the split files back together using the cat command.

cat x* > merged.txt      # default
cat part_* > merged.txt  # with prefix
ShellScript

These commands will merge the split files (whether named xaa, xab, etc., or part_00, part_01, etc.) back into a single file called merged.txt.

Figure 5. Merging split files with the cat command
Figure 5. Merging split files with the cat command

Useful Applications of the split Command

The split command is particularly useful for managing large files. By splitting a file into smaller parts, you can more easily transfer or back it up. Here are some common use cases:

  • During network transfers: Large files are at risk of failing or timing out during network transfers. By splitting the file, you can transfer it in smaller parts, reducing the risk of failure. In case of an error, you only need to retransmit the failed part.
  • Managing storage space: When working with limited storage, it’s easier to save or back up parts of a file rather than trying to store a single, large file.

Important Considerations

  1. When splitting a file, keep in mind that the format of the original file may not be preserved in the split files. This is especially important for non-text files.
  2. Files split using the split command must be merged back together before they can be used properly, especially when transferring to another system. Make sure to provide clear instructions on how to merge the files if you’re sending them to someone else.
  3. If you split a file into many parts, managing those files can become complex. Make sure to organize your prefixes and filenames in a logical manner to avoid confusion.

Summary

The Linux command split is a powerful tool for splitting large files, making them easier to manage and transfer. By mastering the basic usage and various options, you can easily split files by size or line count, set custom prefixes for output files, and even merge them back together when needed. Whether you’re managing large system logs or transferring data across a network, the split command is an essential tool for effective file management in Linux.

References

Leave a Comment