The split command in Linux is a useful tool for splitting large files into smaller parts. This is useful when dealing with large files that are too big to be transferred or managed easily. The split command can be used to split a file based on a specified size or line count, making it an ideal tool for breaking down large files into more manageable parts.

In this tutorial, we will cover the basics of the split command, including its syntax and options, to help you understand how it works and how to use it effectively.

Syntax

The basic syntax for the split command is as follows:

split [options] [inputfile] [prefix]

Here:

  • `[options]`: The options specify how the file should be split, such as the size or line count for each split file.
  • `[input-file]`: The input file is the file that you want to split.
  • `[prefix]`: The prefix is the name that will be given to the split files, followed by a suffix (e.g. xaa, xab, etc.).

Options:


Here are some of the most commonly used options for the split command:

  • -b size: Specifies the size of each split file, in bytes. For example, -b 100M will split the file into 100 MB parts.
  • -l lines: Specifies the number of lines in each split file. For example, -l 1000 will split the file into parts with 1000 lines each.
  • -a suffix_length: Specifies the length of the suffix used for the split files. The default suffix length is 2.

Examples

Let’s go through a few examples to see how the split command works in practice.

  1. Split a file into 100 MB parts
    split -b 100M file.txt split_ 
    

    This command will split the file file.txt into parts with a maximum size of 100 MB each, and the split files will be named split_aa, split_ab, etc.

  2. Split a file into 1000 line parts
    split -l 1000 file.txt split_ 
    

    This command will split the file file.txt into parts with a maximum of 1000 lines each, and the split files will be named split_aa, split_ab, etc.

  3. Change the suffix length
    split -a 3 -b 100M file.txt split_ 
    

    This command will split the file file.txt into parts with a maximum size of 100 MB each, and the suffix length will be 3 characters. The split files will be named split_aaa, split_aab, etc.

  4. Change the file prefix
    split -a 3 -b 100M file.txt backup_ 
    

    This command will create the splitter file, that name will begin with “backup_” prefix. You can change this with any string of your choice. Which will be used as an identifier of the files.

Conclusion

In conclusion, the split command in Linux is a versatile and useful tool for splitting large files into smaller parts. By understanding its syntax and options, you can effectively split files based on size or line count, making it easier to manage and transfer large files. Whether you’re backing up data or transferring large amounts of data between systems, the split command is a valuable tool to have in your Linux toolkit.