← Full reference

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

OptionWhat it does
-l NN lines per piece
-b SIZESIZE bytes each (100K, 10M, 1G; KB, MB — powers of 1000). Lines are cut in the middle
-C SIZEat most SIZE bytes, without breaking lines where possible; a line longer than SIZE is still cut
-n Nexactly N pieces of equal size in bytes (lines are cut)
-n l/NN pieces without breaking lines
-n r/Ndeal lines round-robin: the 1st to the first piece, the 2nd to the second…
-n K/Ncreate no files, print only the K-th of N pieces to stdout (also l/K/N, r/K/N)
-t charrecord separator instead of newline (-t ',', -t '\0')
-ewith -n, don't create empty pieces

Piece names

OptionWhat it does
-a Nsuffix length (default 2: aa…zz — 676 pieces)
-dnumeric suffixes starting from zero: 00, 01…
--numeric-suffixes=1numeric, starting from the given number
-xhexadecimal suffixes: 00…0f…
--additional-suffix=.txtappend an extension after the suffix: part_00.txt
--verbosereport 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 only xaa, then xab and xac remain from the last run, and cat x* assembles garbage. Delete old pieces or write into a clean directory.
  • Suffixes run out. With an explicit -a 1 you can't make more than 26 pieces: "output file suffixes exhausted", code 1. If -a isn't given, GNU split lengthens the names itself: after xyz come xzaaa, xzaab… (with -d: after x89 — x9000), and the sort order stays correct.
  • -b and -n N cut lines in the middle. For text that will be processed line by line, use -l, -C or -n l/N.
  • Write --filter in single quotes so that $FILE is substituted by split, not by the shell beforehand.
  • Assembly order. cat part_* assembles in alphabetical order — correct for split's suffixes, but not for your own names like part_1, part_10, part_2.
  • A prefix with a path: split -l 100 big.log /tmp/out/part_ — the /tmp/out directory must exist.

See also

cat · head-tail · wc · dd · gzip · checksums · xargs