csplit Command in Linux (original) (raw)

Last Updated : 31 Jan, 2026

The csplit command in Linux is used to split a file into multiple smaller files. You can specify where to split the file, such as after a specific line number or when a particular word or pattern is found. It is mainly useful when working with large text files that need to be divided into meaningful sections.

Example 1: Split a File at a Specific Line Number

This example splits a file into two parts starting from a given line number. Consider a text file named 'list.txt' with contents as follows:

**Command:

csplit list.txt 2

**Note : Output files are created as xx00, xx01 in the current directory.

Example 2: Split a File Using a Text Pattern

This command splits a file when a specific word or text is found.

**Command:

csplit list.txt /Papaya/

csplit-pattern

Split a File Using a Text Pattern

Example 3: Split a File into Multiple Parts Using Line Numbers

You can split a file into more than two parts by specifying multiple line numbers.

**Command:

csplit list.txt 2 5

csplit-multi-line

Split a File into Multiple Parts

Syntax

csplit [options] filename pattern...

**Notes: Line numbers start from 1. Text patterns should be written between slashes, e.g., /pattern/. By default, output files are named xx00, xx01, xx02, …, csplit prints the size (in bytes) of each split file to the terminal.

Options for the csplit Command

The csplit command provides several options to control output file naming, handle errors, and remove empty files. Below are the most commonly used options.

1. -f, --Prefix:

This option allows you to give a custom name prefix to all output files instead of the default xx. It is helpful when splitting multiple files or when you want the split files to have meaningful names for easier identification.

**Command:

csplit -f abc file.txt 2

2. -k, --keep-files : Keep Files on Error

By default, if csplit encounters an error while splitting a file, it deletes all partially created output files. Using -k ensures that all files created before the error are kept, which is useful if you do not want to lose work or want to inspect partial outputs.

**Command:

csplit -k file.txt 2 {3}

3. -n, --digits : Number of Digits in File Names

This option allows you to control the number of digits used in the suffix of output files. It is especially useful when splitting a file into many parts, so the naming stays consistent and files are sorted correctly in the directory.

**Command:

csplit -n 1 file.txt 2

**4. -z, --elide-empty-files : Remove Empty Files

When splitting a file, sometimes an output file may be empty (for example, if the pattern occurs at the end of the file). Using -z prevents creating these empty files, keeping only files that contain actual content. This is useful for cleaning up directories and avoiding unnecessary empty files.

**Command:

csplit -z file.txt 4

5. -s, --quiet : Suppress Byte Count Output

This option hides the byte counts of the split files from the terminal. It is useful when you only want the files created and do not care about their sizes. It also keeps the output clean and is helpful when running csplit in scripts or automated tasks.

**Command:

csplit -s list.txt 2 5

csplit-s

Suppress byte count

6. -b FORMAT, --suffix-format=FORMAT : Customize File Suffix Format

This option allows you to change the numeric suffix format of output files using a sprintf style format (for example %03d, %04d). It is helpful when splitting a file into many parts, so the files are consistently named and easy to sort.

**Command:

csplit -b "%03d" list.txt 2 5

csplit-b

Customized file suffix format

Real-World Use Cases

The csplit command is commonly used when working with large text files that need to be divided into smaller, meaningful parts for easier processing and analysis.

Use Case 1: Split a Log File by Error Messages

System log files often contain repeated error sections. You can split the log file each time an error appears.

csplit system.log /ERROR/

Use Case 2: Split a File into Fixed Sections Using Line Numbers

When a file follows a fixed structure, you can split it using line numbers.

csplit report.txt 50 100

Use Case 3: Split Configuration Files into Logical Parts

Large configuration files can be split based on section headers.

csplit config.conf /DATABASE/ /NETWORK/

Use Case 4: Use csplit in Scripts Without Terminal Output

When using csplit inside scripts, you may want clean output.

csplit -s data.txt 10 20