split – Split files of any size

The split command is used to split a large file into smaller files of equal size or a specified number of lines. The command is useful when transferring or storing large files that cannot fit into a single storage device or when sending large files over email.

Overview

The basic syntax of the split command is as follows:

split [OPTION]... [INPUT [PREFIX]]
  • [OPTION]: specifies the options to use with the split command.
  • [INPUT]: specifies the input file to split. If no input file is specified, the command reads from standard input.
  • [PREFIX]: specifies the prefix for the output files. If no prefix is specified, the default prefix is “x”.

Examples

  1. Split a file into smaller files of equal size:
split -b 1M largefile.txt smallfile

This command splits the file “largefile.txt” into smaller files with a size of 1MB each. The output files are named “smallfileaa”, “smallfileab”, “smallfileac”, and so on.

  1. Split a file into smaller files with a specified number of lines:
split -l 1000 largefile.txt smallfile

This command splits the file “largefile.txt” into smaller files with 1000 lines each. The output files are named “smallfileaa”, “smallfileab”, “smallfileac”, and so on.

  1. Split a file into smaller files with a custom prefix:
split -b 1M largefile.txt myfiles

This command splits the file “largefile.txt” into smaller files with a size of 1MB each. The output files are named “myfilesaa”, “myfilesab”, “myfilesac”, and so on.

Options

The following table lists the available options for the split command:

Option Description
-a, --suffix-length=N Use suffixes of length N (default 2)
-b, --bytes=SIZE Split files into pieces of SIZE bytes
-C, --line-bytes=SIZE Split files into pieces of SIZE bytes, but do not split lines
-d Use numeric suffixes starting from 0 instead of alphabetic suffixes
-l, --lines=NUMBER Split files into pieces of NUMBER lines
-n, --number=CHUNKS Split files into CHUNKS pieces

Troubleshooting Tips

  • If the output files already exist, the split command will overwrite them without warning. To avoid overwriting existing files, use a different prefix or move the existing files to a different directory.
  • If the input file is not specified, the split command will read from standard input. This means that you need to provide input to the command through the keyboard or a pipe.

Notes

  • The split command is available on most Linux distributions and Unix-like systems.
  • The default size of the output files is 1,000 lines or 1,024 bytes, depending on the option used.
  • The output files are named with a two-letter suffix by default. You can change the length of the suffix using the -a option.