Linux split command
split is a built-in file-splitting tool in Linux that can split a file by number of lines, size, or a specified count.
split The command is used to split a large file into several smaller files for easier transfer, storage, or parallel processing.
The split small files can be merged back into the original file using the cat command.
When a log file is too large to open in an editor, when you need to upload a large file in chunks, or when you want to process data in parallel,split is the preferred solution.
Split files are named by default with xaa, xab, xac and other letter sequences.
splitIt does not delete or modify the original file, so splitting is safe. However, when splitting into many small files, make sure there is enough disk space.
Command syntax
split The basic syntax is as follows:
split [选项] [输入文件] [输出文件前缀]
If no input file is specified, data is read from standard input.
If no output file prefix is specified, the default used is x as the prefix.
split The common options are summarized below:
| Option | Function | Example |
|---|---|---|
-b, --bytes=大小 |
Split a file by a specified number of bytes | split -b 100M large.log |
-l, --lines=行数 |
Split a file by a specified number of lines | split -l 1000 data.txt |
-n, --number=数量 |
Split into a specified number of small files | split -n 5 data.txt |
-a, --suffix-length=N |
Specify the suffix length (default is 2) | split -a 3 -l 100 data.txt |
-d, --numeric-suffixes |
Use numeric suffixes instead of letter suffixes | split -d -l 100 data.txt |
-C, --line-bytes=大小 |
Split by size while keeping each line intact | split -C 10M log.txt |
--verbose |
Show detailed information about the splitting process | split --verbose -b 10M file.bin |
Supported size units:K (KB), M (MB), G (GB), T(TB) and other suffixes.
The difference between -b and -C:
| Option | Behavior | Applicable scenarios |
|---|---|---|
-b |
Cuts strictly by byte count and may truncate in the middle of a line | Binary files and scenarios where line breaks do not matter |
-C |
Tries to cut at line boundaries when reaching the size limit, keeping every line intact | Text logs, CSV, and other scenarios that need to keep lines intact |
When splitting text files, prefer using
-Cinstead of-bso that no line is truncated across two files.
Detailed usage
Split by number of lines
Splitting by number of lines is the most intuitive method, suitable for log files or CSV data.
# Generate a test file with 5000 lines
$ seq 1 5000 > data.txt
# Split into a small file every 1000 lines
$ split -l 1000 data.txt part_
# View generated files
$ ls -lh part_*
-rw-r--r-- 1 runoob runoob 3.9K May 19 14:30 part_aa
-rw-r--r-- 1 runoob runoob 3.9K May 19 14:30 part_ab
-rw-r--r-- 1 runoob runoob 3.9K May 19 14:30 part_ac
-rw-r--r-- 1 runoob runoob 3.9K May 19 14:30 part_ad
-rw-r--r-- 1 runoob runoob 3.9K May 19 14:30 part_ae
After running, 5 files are generated, each with exactly 1000 lines, whose names start with part_ the prefix.
Split by file size
Splitting by size is suitable for cutting binary files or for controlling the maximum size of each file.
# Generate a 10MB test file
$ dd if=/dev/urandom of=test.bin bs=1M count=10
# Split every 2MB
$ split -b 2M test.bin chunk_
# View split results
$ ls -lh chunk_*
-rw-r--r-- 1 runoob runoob 2.0M May 19 14:32 chunk_aa
-rw-r--r-- 1 runoob runoob 2.0M May 19 14:32 chunk_ab
-rw-r--r-- 1 runoob runoob 2.0M May 19 14:32 chunk_ac
-rw-r--r-- 1 runoob runoob 2.0M May 19 14:32 chunk_ad
-rw-r--r-- 1 runoob runoob 2.0M May 19 14:32 chunk_ae
Specify the number of splits
Use -n You can divide a file into a specified number of small files evenly.
# Divide the file into 3 parts
$ split -n 3 data.txt equal_
# View line count of each file
$ wc -l equal_*
1667 equal_aa
1667 equal_ab
1666 equal_ac
5000 total
You can see the file is distributed as evenly as possible; a file of 5000 lines total is split into about 1667 lines per part.
When the total number of lines is not evenly divisible, the earlier files get a few more lines and the later files a few fewer.
Use numeric suffixes
The default letter suffixes (aa, ab...) are not intuitive; you can use -d to switch to numbers.
# Use a numeric suffix, and specify the suffix length as 3
$ split -d -a 3 -l 1000 data.txt runoob_
# Generated filename
$ ls runoob_*
runoob_000 runoob_001 runoob_002 runoob_003 runoob_004
Here a suffix length of 3 means up to 1000 files are supported (from 000 to 999); when splitting into many small files you can increase -a value.
Split text while keeping lines intact
Use -C You can split by size while ensuring no line is truncated.
# Generate a test log containing lines of different lengths
$ for i in $(seq 1 100); do echo "Line $i: RUNOOB testing data $(head -c $((RANDOM % 50 + 10)) /dev/urandom | base64)"; done > log.txt
# Split into 1KB, while maintaining line integrity
$ split -C 1K log.txt log_
# Check if the last line of each file is complete (ends with a newline character)
$ for f in log_*; do echo "$f: $(tail -c 1 $f | xxd | grep -c '0a')"; done
-C This ensures the last line of each split file is complete, so no line is cut in half.
Merge the split files
The split files can be merged back losslessly:
# Merge all split files to restore the original file
$ cat part_* > restored.txt
# Verify merged content matches original file
$ diff data.txt restored.txt && echo "文件一致,合并成功"
文件一致,合并成功
When merging, cat files are concatenated in the alphabetical order of shell wildcards, which is exactly the same as split the order in which they were generated.
Common questions
The split files still occupy the same total disk space as the original file (in fact, there is a bit more overhead from filesystem metadata), so make sure there is enough free disk space.
When using numeric suffixes, if the number of split files exceeds the maximum the suffix length can represent (such as
-a 2at most 100 files),splitit will exit with an error. Increase the-avalue of the parameter to solve this.
If you need to split by specific content (such as a delimiter),split is not supported; use csplit command.
When reading data from standard input,split the total size cannot be estimated, so -n the "specified number" feature is not supported in standard input mode.
Related commands
| Command | Function |
|---|---|
cat |
Merge files or output file contents |
csplit |
Split files by content (regular expression) |
wc |
Count the lines, words, and bytes of a file |
dd |
Convert and copy files, and truncate data by block size |
head |
Output the beginning of a file |
tail |
Output the end of a file |
Linux Command Reference