Linux csplit Command: Split Files at Lines or Patterns
csplit splits a file not by a fixed size, but at the line number or the location where a matching pattern appears. It is useful for separating text with section boundaries.
What is the csplit command?
By specifying the split points and file name prefix, you can create pieces according to the structure of the document. Since the default output file names are generated like xx00, use a dedicated directory and prefix that do not conflict with other working files.
Basic syntax
csplit [options] input_file pattern...
Installed implementations and options may vary depending on the distribution. Check the description on the current system with man csplit.
Examples
Splitting by line number
The second file starts from line 11. The first file contains the first 10 lines.
csplit -f part- notes.txt 11
Splitting by pattern
A new piece starts from the line where the pattern appears.
csplit -f section- report.txt '/^Chapter /'
Repeated splitting in multiple matches
GNU {*} repeats using the match afterwards.
csplit -f chapter- book.txt '/^Chapter /' '{*}'
Viewing files divided by pattern
csplit splits the input at the point where the specified line pattern starts. -s suppresses the byte count normally output for each piece.
printf 'one\nTWO\ntwo\n' > sample.txt csplit -s -f part- sample.txt '/^TWO$/' cat part-00 cat part-01
Example output of running two cat commands in sequence:
one TWO two
part-00 contains one before the pattern, and part-01 includes everything from the pattern line to the end. If the pattern is not found, the command may fail, and existing split files could be overwritten, so use sample files and output prefixes in a separate working directory.
Main Options and Format
| Options/Format | Description |
|---|---|
-f prefix |
Sets the prefix of output piece filenames. |
-n N |
Sets the number of digits of the numeric suffix. |
-s |
Hides byte size output of each piece. |
-z |
Does not create empty output files. |
-k |
Keeps already created pieces even if an error occurs. |
Precautions when using
If there is no pattern or the boundary differs from expectations, the number and position of pieces will vary. Check the results first with a test input, and do not run it in a directory where output files with existing names are present. Unlike split, the splitting rules depend on the file content itself.
Frequently Asked Questions
Which should you use, split or csplit?
If you want to split evenly by size or number of lines, use split; if you want to split at specific line numbers or title patterns, csplit is appropriate.
Official Documentation
You can check the exact behavior of options and differences between implementations in the official csplit documentation.









