csplit command in Linux with examples

In Linux, we can employ many different methods for making text file management more convenient and fluid. The csplit command in Linux is a perfect example of how we can make text files a lot easier to maintain.

The base function of the csplit command is to, as the name implies, split the data of a given text file into multiple separate files. By default, this command will split the data of a file based on context lines and then store them in new text files named xx00 and xx01.

In this tutorial, we’ll show you with easy to follow examples the best way to utilize the csplit command and its various command line options.

In this tutorial you will learn:

  • How to use the csplit command on Linux
csplit command in Linux with examples
csplit command in Linux with examples
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software csplit
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Frequently Used Options




The csplit command will split a file into sections determined by context lines. Check out the examples below to see how it works with its most frequently usd options.

csplit command in Linux Basic Examples

  1. If we have a text file and want to split its data evenly across two text files, we would pass the csplit command a number that specifies how many lines we want to be spread across two text files.
    $ csplit file01 3
    
    Split data across multiple files based on context lines
    Split data across multiple files based on context lines

    As you can see in the screenshot above, our example text file file01 has four lines. These lines each contain one number from 1 to 4. We then used the csplit command to send half that data to xx00 and half to xx01. We used the number 3 in the syntax above to tell csplit command to start the split on the third line.

  2. As you’ve observed in the example above, upon splitting the data, two files starting with “xx” were created. You can use the -f or --prefix command line options to change these prefixes.
    $ csplit -f test file01 3
    

    Using the csplit command with the -f option to specify the prefix
    Using the csplit command with the -f option to specify the prefix


  3. In addition to fully customizing the prefix of the filenames the csplit command creates, we can also use the -n or the --digits command line options to customize how the csplit command numbers these newly created text files.
    $ csplit -n 3 file01 3
    
    Using the -n command line option to customize file name numbering
    Using the -n command line option to customize file name numbering

    As you can see in the screenshot above, the file names of the text files that were created now possess a numbering of “0,1” instead of “00,01” as they did in the previous examples.

Advanced Usage

The csplit command is pretty simple, but as you’ve observed throughout the examples section of this article, it does not come packed with a lot of options. This is because the average Linux user may not need to use the csplit command frequently, as it is for very specific scenarios. However, it can definitely come in handy when the situation calls for it. In this section of the tutorial, we’ll show you a few of the lesser known options of the csplit command that we think are useful.

csplit command in Linux Advanced Examples



  1. Sometimes when you utilize the csplit command to separate data in a text file across other text files, you can get empty output files. We can prevent this from happening by evoking the -z or --elide-empty-files command line options.
    $ csplit -z file01 3
    

    Using the csplit command with the -z option to remove empty output files
    Using the csplit command with the -z option to remove empty output files
  2. We can also use the --suppress-matched command line option to omit a specified line when creating the new text files.
    $ csplit --suppress-matched file01 3
    
    Using --suppress-matched command line option to omit a specified line
    Using –suppress-matched command line option to omit a specified line

    As you can see in the screenshot above, line 3, which is the number we specified in the syntax above, is not present in any of the new text files created.

Closing Thoughts




In this tutorial, we learned all about the csplit command on Linux which is beneficial to master for users and administrators who tend to view text files in the Linux terminal frequently. But again, if you are a relatively average Linux user might not use this command on a daily basis. But this does not mean that it isn’t worth remembering that it exists!