csplit and split Command Examples in Linux (Cheat Sheet)

Tech reviewed: Deepak Prasad
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

Task split Command
Split file into smaller parts split filename
Split file into fixed number of lines split -l 1000 filename
Split file by file size split -b 10M filename
Split file into N equal parts split -n 5 filename
Set custom output file prefix split -l 1000 filename part_
Split file and use numeric suffix split -d filename part_
Set suffix length split -a 3 filename part_
Split large log file split -b 100M logfile.log
Split file into chunks for transfer split -b 500M bigfile.iso
Recombine split files cat 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

Task csplit Command
Split file based on pattern csplit filename '/pattern/'
Split file at specific line number csplit filename 100
Split file at multiple line numbers csplit filename 100 200
Split file using regex pattern csplit filename '/Chapter/'
Keep original file after split csplit -k filename '/pattern/'
Suppress output messages csplit -s filename '/pattern/'
Set custom output prefix csplit -f part filename '/pattern/'
Set suffix length csplit -n 3 filename '/pattern/'
Split file multiple times by pattern csplit filename '/pattern/' '{*}'
Split file based on repeating section csplit 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 Case csplit split
Primary logic Context-based (content-aware) Size-based (quantity-aware)
Split trigger Regex, patterns, line numbers Bytes, KB, MB, line counts
Best suited for Logs, SQL dumps, structured records Large files, archives, CSVs
Output file naming xx00, xx01 (default) xaa, xab (default)
Custom suffix support Yes (printf-style, e.g. %02d) Yes (alphabetic or numeric with -d)
Complexity Higher (regex knowledge required) Low (plug-and-play)
Structure aware Yes (preserves logical blocks) No (can split mid-line)

When to Use csplit vs split

Scenario Prefer
Split XML, logs, or config blocks csplit
Split using regex or marker lines csplit
Preserve logical sections csplit
Split large files for transfer split
Split by size (MB/GB) split
Split by fixed number of lines split

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

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, …

  • Red Hat Certified System Administrator in Red Hat OpenStack
  • Certified Kubernetes Application Developer (CKAD)
  • Red Hat Certified Specialist in Ansible Automation
  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security