VMware ESXi and vSphere Cluster Management
Split a File into Two or More Files in Linux
Learn how to use the Linux split command to divide one file into smaller pieces by byte size or line count, inspect the results, and choose the right method.
The Linux split command separates one input file into multiple output files. It is useful when a file is too large to transfer, store, or process conveniently as a single item.
Splitting creates file fragments. It does not automatically compress, archive, or merge data. Each generated file contains part of the original input.
What the split Command Does
The input file is the original file supplied to split. The command reads that file and creates several smaller output files.
Common reasons to split a file include:
- Transferring pieces through a system with a file-size limit.
- Storing portions on media with limited available space.
- Processing smaller chunks instead of loading a very large file at once.
- Preparing separate portions of a text file for different uses.
A split file is not a backup archive and is not compressed. If you need compression or archiving, use a separate tool for that purpose.
Basic Command Structure
A common command pattern is:
split [option] input_file output_prefix
The option selects how the input is divided. The input filename identifies the original file. The final argument is the output prefix, which determines the initial part of every generated filename.
By default, split appends alphabetic suffixes to the prefix. The sequence normally begins with aa, then ab, ac, and continues as more pieces are needed. If no prefix is supplied, the default prefix is commonly x, producing names such as xaa and xab.
| Output prefix | Generated suffix | Resulting filename |
|---|---|---|
new_file_ | aa | new_file_aa |
new_file_ | ab | new_file_ab |
new_file_ | ac | new_file_ac |
new_file_ | ad | new_file_ad |
Choose a Splitting Method
| Option | Meaning | Unit or behavior | Appropriate use |
|---|---|---|---|
-b | Split by byte size | Each piece is limited to a specified number of bytes. | Size limits, transfers, and arbitrary binary or data chunks. |
-l | Split by line count | Each piece contains a specified number of newline-delimited lines. | Logs, lists, text files, and records that must remain whole lines. |
A byte is a unit of file data. A line is a newline-delimited section of a text file. Byte-based splitting can divide content at any position, including in the middle of a line or record. Line-based splitting preserves line boundaries.
Split a File by Byte Size
Use the -b option when each output piece should be no larger than a specified number of bytes.
split -b 55 file.txt new_file_
This command reads file.txt and creates pieces of up to 55 bytes. The output prefix is new_file_, so the first files are typically:
new_file_aa
new_file_ab
new_file_ac
The final piece can be smaller than 55 bytes. This is normal when the input file size is not an exact multiple of 55 bytes.
Byte splitting is useful when a destination imposes a maximum size. However, it may break a text line, delimiter, or binary structure at an arbitrary position. Make sure the program that consumes the pieces can handle that behavior.
Split a File by Line Count
Use the -l option when each output file should contain a selected number of lines.
To create one output file for every line in file.txt, run:
split -l 1 file.txt new_file_
Each generated file contains one original line. The names use the selected prefix and sequential suffixes, such as new_file_aa, new_file_ab, and new_file_ac.
Line-based splitting is appropriate for line-oriented logs, lists, plain-text documents, and delimited records where each record occupies one complete line. To place several lines in each output file, change the number. For example, -l 100 creates pieces containing up to 100 lines each.
Inspect the Input and Output
Before splitting a text file by lines, display its contents with line numbers:
nl file.txt
The nl command displays text with line numbers, making it easier to verify the order and count of the input lines.
For example, the output might look like this:
1 first record
2 second record
3 third record
After running a split command, list the generated files using the prefix:
ls new_file_*
The wildcard matches filenames beginning with new_file_. Inspect the pieces individually if you need to confirm their contents.
Byte Splitting Versus Line Splitting
- Use
-bwhen a maximum data size matters more than content boundaries. - Use
-lwhen every output file must contain complete lines or records. - Use byte splitting for arbitrary data only when dividing at any byte is acceptable to the receiving process.
- Use line splitting for logs, lists, and line-delimited data that should remain readable and structurally intact.
Troubleshooting
Unexpected Names Such as xaa and xab
If the output files are named xaa and xab, no custom output prefix was supplied. Provide the prefix as the final argument:
split -b 55 file.txt new_file_
The Last Piece Is Smaller
The final output file may be smaller than the requested byte size or contain fewer lines than requested. This occurs when the input length is not an exact multiple of the selected size or line count. It is normal behavior.
A Text Record Was Divided in an Inconvenient Place
This usually means byte-based splitting was used for content that needs line boundaries. Use line-based splitting instead:
split -l 1 file.txt new_file_
Choose a larger line count when several complete records should be grouped in each piece.
Existing Files Cause Confusion
If files with the selected prefix already exist, they can be confused with the new results. Inspect matching names first:
ls new_file_*
Choose a unique output prefix, or move unrelated files before running the command.
The Source File Cannot Be Read
Check the filename, path, and permissions. The input file must exist, and your user must have permission to read it. A spelling or path error can also cause the command to fail.
Quick Reference
# Inspect numbered input lines
nl file.txt
# Split into pieces of up to 55 bytes
split -b 55 file.txt new_file_
# Split into one file per line
split -l 1 file.txt new_file_
# List generated pieces
ls new_file_*
Use a clear, unique output prefix and select -b for size-based chunks or -l when preserving complete text lines is important.