![]() |
VOOZH | about |
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.
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:
👁 ImageCommand:
csplit list.txt 2👁 ImageNote : Output files are created as xx00, xx01 in the current directory.
This command splits a file when a specific word or text is found.
Command:
csplit list.txt /Papaya/You can split a file into more than two parts by specifying multiple line numbers.
Command:
csplit list.txt 2 5csplit [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.
The csplit command provides several options to control output file naming, handle errors, and remove empty files. Below are the most commonly used options.
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 2By 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}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 2When 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 4This 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 5This 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 5The 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.
System log files often contain repeated error sections. You can split the log file each time an error appears.
csplit system.log /ERROR/When a file follows a fixed structure, you can split it using line numbers.
csplit report.txt 50 100Large configuration files can be split based on section headers.
csplit config.conf /DATABASE/ /NETWORK/When using csplit inside scripts, you may want clean output.
csplit -s data.txt 10 20