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.

More in This Category
Linux awk Command: Analyze Text by Fields

Linux awk Command: Analyze Text by Fields

Learn how to analyze Text by Fields with the Linux awk command, including practical examples, key options, and important precautions.

Linux unzip Command: List and Extract ZIP Archives

Linux unzip Command: List and Extract ZIP Archives

Learn how to list and Extract ZIP Archives with the Linux unzip command, including practical examples, key options, and important precautions.

Linux mkdir Command: Create Directories and Parent Paths

Linux mkdir Command: Create Directories and Parent Paths

Learn how to create directories with Linux mkdir, build missing parent paths, set permissions, understand umask behavior, and verify results in scripts.

Linux getent Command: Query Users, Groups, and Other NSS Databases

Linux getent Command: Query Users, Groups, and Other NSS Databases

Learn how to query Users, Groups, and Other NSS Databases with the Linux getent command, including practical examples, key options, and important precautions.

Linux ethtool Command: Inspect Network Link and Driver Settings

Linux ethtool Command: Inspect Network Link and Driver Settings

Learn how to inspect Network Link and Driver Settings with the Linux ethtool command, including practical examples, key options, and important precautions.

Linux bzip2 Command: Compress a File in bzip2 Format

Linux bzip2 Command: Compress a File in bzip2 Format

Learn how to compress a File in bzip2 Format with the Linux bzip2 command, including practical examples, key options, and important precautions.

Linux chmod Command: Change File and Directory Permissions

Linux chmod Command: Change File and Directory Permissions

Learn how to change File and Directory Permissions with the Linux chmod command, including practical examples, key options, and important precautions.

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Linux Tutorial / PID and PPID Explained: Find and Manage Processes

Understand what PID and PPID mean, inspect parent-child process relationships with ps and pgrep, and safely terminate a process you started yourself.

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Linux Tutorial / Search and Process Text with grep, sort, uniq, cut, and wc

Practice searching, sorting, grouping, extracting, and counting text with grep, sort, uniq, cut, and wc, including reproducible output and key caveats.

Linux Tutorial / Understand systemd Services, Boot, and Logs

Linux Tutorial / Understand systemd Services, Boot, and Logs

Understand Linux boot stages and systemd units, distinguish active services from boot enablement, and inspect current-boot logs with systemctl and journalctl.