Text and processing
The split command
Splits a large file into smaller pieces, by number of lines, by bytes or into a fixed number of parts. Useful for uploads with a size limit or for dividing a job across several machines.
Most useful options
-l NEach piece has N lines. The usual choice for text, because it never cuts a line in half-b TAMEach piece has SIZE bytes. Accepts suffixes such as K, M or G-C TAMLike -b, but without cutting lines: each piece holds up to SIZE bytes-n NSplits into N pieces of almost equal size. With l/N it divides by lines and cuts none-dUses numeric suffixes (00, 01...) instead of letters (aa, ab...)-a NSuffix length. With -a 3 the names become aaa, aab...--additional-suffix SUFIJOAppends a fixed ending to each piece, for example .txtCopy-ready examples
$ split -l 4 diez.txt trozo_ && ls trozo_*splits the file into blocks of four lines. The names are the prefix plus aa, ab, ac...Example output
trozo_aa trozo_ab trozo_ac
$ cat trozo_abthe second piece holds lines 5 to 8 of the original.Example output
5 6 7 8
$ split -n l/2 --additional-suffix=.txt diez.txt mitad_ && ls mitad_*two halves by lines, without cutting any, with the .txt extension.Example output
mitad_aa.txt mitad_ab.txt
You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| split: cannot open 'nonexist' for reading: No such file or directory | The source file does not exist or the name is misspelled. | Check the name with ls, respecting upper and lower case. |
| split: carpeta: Is a directory | You gave a directory as the source. split only works with files. | Pack the directory with tar and split the resulting archive. |
| split: invalid number of lines: 'abc' | The value given to -l is not a whole number. | Write digits only, for example -l 1000. |
| split: invalid number of lines: '0': Numerical result out of range | Zero lines per piece makes no sense. | Use a number greater than zero. |
Typical mistakes
- With -b or -n without l/ it can cut a line in half. For text files use -l or -C.
- split does not delete the original or check free space: the pieces take up the same total size as the file.
- If the output names already exist, split overwrites them without asking.
Differences from similar commands
csplit: splits at patterns or specific line numbers, for example before each heading. split only cuts by size or number of lines.- head: prints only the first lines without creating any file. split creates every piece on disk.
FAQ
How do I join the pieces back together?
With cat trozo_* > full_file. The asterisk sorts the pieces by suffix, which is the correct order.
Why are the pieces named aa, ab, ac?
They are alphabetic suffixes by default. With -d they become numeric, and with -a 3 they get longer.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course