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 stat Command: Display Detailed File Metadata

Linux stat Command: Display Detailed File Metadata

Learn how to use the Linux stat command to inspect file size, permissions, ownership, timestamps, inode, and filesystem metadata, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux join Command: Combine Files by a Common Field

Linux join Command: Combine Files by a Common Field

Learn how to combine Files by a Common Field with the Linux join command, including practical examples, key options, and important precautions.

Linux nice Command: Set a New Process's Scheduling Priority

Linux nice Command: Set a New Process's Scheduling Priority

Learn how to set a New Process's Scheduling Priority with the Linux nice command, including practical examples, key options, and important precautions.

Linux unxz Command: Decompress xz Files

Linux unxz Command: Decompress xz Files

Learn how to decompress xz Files with the Linux unxz command, including practical examples, key options, and important precautions.

Linux fdisk Command: Inspect and Edit Disk Partitions

Linux fdisk Command: Inspect and Edit Disk Partitions

Learn how to inspect and Edit Disk Partitions with the Linux fdisk 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 mv Command: Move and Rename Files and Directories

Linux mv Command: Move and Rename Files and Directories

Learn how to move and rename files or directories with Linux mv, avoid unwanted overwrites, clarify destination paths, and handle cross-filesystem moves.

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 tee Command: Display and Save Pipeline Output

Linux tee Command: Display and Save Pipeline Output

Learn how to display and Save Pipeline Output with the Linux tee command, including practical examples, key options, and important precautions.

Linux groups Command: Show a User's Group Memberships

Linux groups Command: Show a User's Group Memberships

Learn how to show a User's Group Memberships with the Linux groups command, including practical examples, key options, and important precautions.