csplit and split Command Examples in Linux (Cheat Sheet)

csplit and split Command Examples in Linux (Cheat Sheet)

Linux provides two powerful utilities to split files into smaller parts: csplit and split.

  • csplit → pattern and structure aware
  • split → size, line count, or fixed chunks

This guide explains when to use which, with practical examples.


split Command - Quick Cheat Sheet

Tasksplit Command
Split file into smaller partssplit filename
Split file into fixed number of linessplit -l 1000 filename
Split file by file sizesplit -b 10M filename
Split file into N equal partssplit -n 5 filename
Set custom output file prefixsplit -l 1000 filename part_
Split file and use numeric suffixsplit -d filename part_
Set suffix lengthsplit -a 3 filename part_
Split large log filesplit -b 100M logfile.log
Split file into chunks for transfersplit -b 500M bigfile.iso
Recombine split filescat part_* > original_file

This quick reference summarizes the most commonly used split command examples in Linux for dividing large files into smaller parts.


csplit Command - Quick Cheat Sheet

Taskcsplit Command
Split file based on patterncsplit filename '/pattern/'
Split file at specific line numbercsplit filename 100
Split file at multiple line numberscsplit filename 100 200
Split file using regex patterncsplit filename '/Chapter/'
Keep original file after splitcsplit -k filename '/pattern/'
Suppress output messagescsplit -s filename '/pattern/'
Set custom output prefixcsplit -f part filename '/pattern/'
Set suffix lengthcsplit -n 3 filename '/pattern/'
Split file multiple times by patterncsplit filename '/pattern/' '{*}'
Split file based on repeating sectioncsplit filename '/Section/' '{5}'

This quick reference summarizes the most commonly used csplit command examples in Linux for splitting files using patterns or line numbers.


How csplit and split Work

Think of file splitting as a two-step process:

bash
input file  →  split rule  →  output files

The difference lies in how the split rule is defined.

  • csplit looks inside the file content and splits when a pattern is found
  • split ignores content and splits purely by size, lines, or count

In simple terms:

  • csplit asks “where should I cut?”
  • split asks “how big should each piece be?”

csplit vs split (Feature Comparison)

Feature / Use Casecsplitsplit
Primary logicContext-based (content-aware)Size-based (quantity-aware)
Split triggerRegex, patterns, line numbersBytes, KB, MB, line counts
Best suited forLogs, SQL dumps, structured recordsLarge files, archives, CSVs
Output file namingxx00, xx01 (default)xaa, xab (default)
Custom suffix supportYes (printf-style, e.g. %02d)Yes (alphabetic or numeric with -d)
ComplexityHigher (regex knowledge required)Low (plug-and-play)
Structure awareYes (preserves logical blocks)No (can split mid-line)

When to Use csplit vs split

ScenarioPrefer
Split XML, logs, or config blockscsplit
Split using regex or marker linescsplit
Preserve logical sectionscsplit
Split large files for transfersplit
Split by size (MB/GB)split
Split by fixed number of linessplit

Rule of thumb:
If you care what the content is → use csplit
If you care how big the output is → use split


csplit Examples

The csplit command splits files based on patterns or regular expressions, making it ideal for structured files such as XML, logs, or configuration outputs.

Split a file using a regex match

bash
csplit my_file '/^<report>$/' '{*}'

This command splits the file every time a line exactly matching <report> is found.

  • Each <report> ... </report> block is written to a separate file
  • {*} tells csplit to repeat the split until the end of the file

This approach is commonly used for XML, JSON-like logs, or reports where sections are clearly marked.

Split based on a pattern and include the matched line

bash
csplit --elide-empty-files my_file '/000/+1' '{*}'

Here, the file is split whenever the pattern 000 is encountered.

  • +1 ensures the matching line is included in the next output file
  • Useful when the delimiter line belongs to the data block itself

This is ideal for log files or data dumps where separators must be preserved.

Suppress the matched pattern from output files

bash
csplit --suppress-matched my_file '/000/' '{*}'

This variation removes the delimiter line (000) entirely from all output files.

Use this when:

  • The pattern is only a separator
  • You do not want marker lines in the split results

Prevent creation of empty files

bash
csplit --elide-empty-files my_file '/pattern/' '{*}'

Some patterns can produce empty output files during splitting.

The --elide-empty-files option ensures:

  • Only non-empty files are created
  • Cleaner output with fewer unnecessary files

Add a custom prefix and suffix to output files

bash
csplit --prefix part --suffix-format "%02d.txt" my_file '/000/' '{*}'

This generates well-named output files such as:

  • part00.txt
  • part01.txt
  • part02.txt

Using prefixes and formatted suffixes is recommended for:

  • Automation scripts
  • Easier file ordering
  • Cleaner post-processing

Split only a limited number of times

bash
csplit my_file '/000/' '{1}'

By limiting the repeat count:

  • The pattern is applied only once
  • Exactly two files are created

This is useful when you want to split a file into header and body sections instead of multiple fragments.


Frequently Asked Questions

1. What is the difference between split and csplit in Linux?

The split command divides files based on size, line count, or number of output files, while csplit splits files based on patterns or regular expressions found in the file content. Use split for large files and csplit for structured text.

2. When should I use csplit instead of split?

Use csplit when your file has clear markers, headers, or repeated sections such as XML blocks, log entries, or function definitions, and you need content-aware splitting.

3. Can split handle very large files efficiently?

Yes. The split command is optimized for large files and can efficiently divide files by size or line count without loading the entire file into memory.

4. Does csplit support regular expressions?

Yes. csplit supports full regular expressions, allowing you to split files based on complex patterns and structured content.

5. How do I join files back after splitting?

Files created by split or csplit can be recombined using the cat command by concatenating them in the correct order into a new file.

Conclusion

The csplit and split commands solve different file-splitting problems in Linux.

  • Use csplit when the file has a clear structure and you need to split based on patterns, headers, or sections
  • Use split when you need to divide files by size, line count, or into equal parts

Understanding both tools gives you complete control over file splitting and reassembly, making them essential utilities for Linux text processing, automation, and system administration tasks.


Also Read


Further Reading

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.