split
Cut a file into pieces by lines, bytes or number of chunks and put it back together
split cuts a file (or stdin) into pieces: N lines each, N bytes each or into N parts. The pieces are named prefix + suffix: by default xaa, xab, xac… They're joined back with plain cat, because the suffixes go in alphabetical order. The sandbox has GNU coreutils 9.4.
Syntax
split [options] [file [prefix]]
command | split [options] - [prefix]
Without a file or with - it reads stdin. The default prefix is x. Without options — pieces of 1000 lines.
Options
How to cut
| Option | What it does |
|---|---|
-l N | N lines per piece |
-b SIZE | SIZE bytes each (100K, 10M, 1G; KB, MB — powers of 1000). Lines are cut in the middle |
-C SIZE | at most SIZE bytes, without breaking lines where possible; a line longer than SIZE is still cut |
-n N | exactly N pieces of equal size in bytes (lines are cut) |
-n l/N | N pieces without breaking lines |
-n r/N | deal lines round-robin: the 1st to the first piece, the 2nd to the second… |
-n K/N | create no files, print only the K-th of N pieces to stdout (also l/K/N, r/K/N) |
-t char | record separator instead of newline (-t ',', -t '\0') |
-e | with -n, don't create empty pieces |
Piece names
| Option | What it does |
|---|---|
-a N | suffix length (default 2: aa…zz — 676 pieces) |
-d | numeric suffixes starting from zero: 00, 01… |
--numeric-suffixes=1 | numeric, starting from the given number |
-x | hexadecimal suffixes: 00…0f… |
--additional-suffix=.txt | append an extension after the suffix: part_00.txt |
--verbose | report each file created: creating file 'v_aa' |
--filter='command' | don't write files, pass each piece to a command; the piece name is in $FILE |
Examples
# sample data
seq 1 10 > numbers.txt
# 4 lines each: xaa (1–4), xab (5–8), xac (9–10)
split -l 4 numbers.txt
# readable names: part_00, part_01, part_02
split -l 4 -d numbers.txt part_
# with an extension and a three-digit number: chunk_000.csv, chunk_001.csv…
split -l 1000 -d -a 3 --additional-suffix=.csv data.csv chunk_
# a binary file into 100 MiB pieces and back
split -b 100M backup.tar.gz backup.tar.gz.part_
cat backup.tar.gz.part_* > restored.tar.gz
cmp backup.tar.gz restored.tar.gz && echo 'identical'
# into 4 parts without breaking lines — e.g. for parallel processing
split -n l/4 access.log log_
# only the 2nd of 3 parts — to stdout, no files
split -n l/2/3 numbers.txt
# from a pipeline
seq 1 30 | split -l 10 - batch_
# compress each piece right away
split -l 100000 --filter='gzip > $FILE.gz' big.log big_
# CSV with a header: the header goes into every piece
tail -n +2 data.csv | split -l 1000 -d --filter='{ head -1 data.csv; cat; } > $FILE.csv' - part_
Pitfalls
- Old pieces aren't deleted. If you previously got
xaa…xac, and now onlyxaa, thenxabandxacremain from the last run, andcat x*assembles garbage. Delete old pieces or write into a clean directory. - Suffixes run out. With an explicit
-a 1you can't make more than 26 pieces: "output file suffixes exhausted", code1. If-aisn't given, GNU split lengthens the names itself: afterxyzcomexzaaa,xzaab… (with-d: afterx89—x9000), and the sort order stays correct. -band-n Ncut lines in the middle. For text that will be processed line by line, use-l,-Cor-n l/N.- Write
--filterin single quotes so that$FILEis substituted bysplit, not by the shell beforehand. - Assembly order.
cat part_*assembles in alphabetical order — correct forsplit's suffixes, but not for your own names likepart_1,part_10,part_2. - A prefix with a path:
split -l 100 big.log /tmp/out/part_— the/tmp/outdirectory must exist.