csplit is a Linux command that can be used to split a large file into several smaller files/parts, depending on the user’s requirements. These parts are determined by the context lines. By default, csplit splits the file into 1000 lines per file and gives users the option to change the number of split lines according to their requirements. The output split files have names like “xx00” and “xx01”. The csplit command also outputs the number of bytes in each split file/piece as its own or standard output.

Csplit Command Syntax

The syntax for the csplit command is:

csplit [OPTION]… FILE PATTERN

The OPTION in the syntax is Command-line options, which are used to pass parameters to a program. Where FILE is the filename of which a user wants to get the absolute path.

Command-Line Options

Some of the command line options used in Linux are as follows:

  • -f
  • -k
  • -n
  • -z
  • -s
  • -help
  • -version

Using csplit command – examples

In the further guide, I will explain and elaborate on the use of the csplit command explicitly with each command-line option using examples. For the process, you need to follow the following steps:

Step 01: Create a .txt file to split it using the csplit command

Create a file named ‘Demo.txt’ with the following contents as shown in the below-attached image. Use the text editor in your machine and save it in the home folder.

The user can also create a differently named file with the data of his/her own choice or work requirement. He/she can also save it somewhere else other than the home folder. For this particular guide, I am using Demo.txt.

Step 02: Open the Debian 10 terminal

Go the bottom left of the desktop and click on the menu button.

Go to the search bar on the top left.

Type terminal there. Click on the “Multilingual Terminal”.

Clicking on the Multilingual terminal will show you the following display.

Now write the following command in the Terminal for splitting the file into 2 parts. Since the 2nd part is starting from the 3rd line, we will write ‘3’ in the csplit command.

 csplit Demo.txt 3

Look at the below image.

On running the command, the csplit will give you the bytes count in each split file as a standard number. To see whether Demo.txt is split into 2 files or not, write ‘ls’ in terminal and press enter. On pressing enter, it will display all of the files in the home folder. In the home folder, you can see the 2 split files named as ’xx00’ and ‘xx01’. There are 11 bytes in xx00 and 52 bytes in xx01. All these points are highlighted in the below-attached image.

Now to see the contents of the files xx00 and xx01, write the following command in the terminal.

cat xx00 
cat xx01

Running these commands will give you the following output.

Use of csplit command-line options

Option 01: -f, –Prefix

This option uses PREFIX in place of ‘xx’ in the split filename. The syntax for this command-line option is:

csplit -f pre Demo.txt 3

In the above command, -f is the command-line option, ‘pre’ is the prefix that will be used in place of ‘xx’. The user can set any prefix name of his/her choice. For this particular guide, I am setting it as ‘pre’. Demo.txt is the filename as described above in step 02.

By running this command, you will get the following output as shown highlighted in the below-attached image. The files are now named as ‘pre00’ and ‘pre01’.

Option 02: -k, –keep files

This option doesn’t remove the split output files if it faces any error. Let suppose the following example, I am writing the following faulty command in the terminal. csplit –k Demo.txt 4 {3}

Since the above command has an error, but –k option doesn’t remove the split files. After running csplit –k Demo.txt 4 {3}, I will run another faulty command ls xx*, but the output will still display the split filenames. Look at the below-attached image.

Option 03: -n, –Digits

This option fixes the number of digits after the filename. Let’s suppose, if you fixed the digit number as 1, the output filename will be xx0 instead of xx00. The syntax for this command-line option is:

csplit -n 1 Demo.txt 3

In the above command, -n is the command-line option. The user can set any digit number of his/her choice. For this particular guide, I am setting it as ‘1’. Demo.txt is the filename as described above in step 02.

Running this command in the terminal, you will get the following highlighted output.

Option 04: -z, –elide-empty-files

This option removes the empty output files. The syntax for this command-line option is:

csplit -z Demo.txt 6

In the above command, -z is the command-line option. Demo.txt is the filename as described above in step 02. “Since I am giving the command to split the file into 5 parts and the 5th part is starting from the 6th line, hence there will be empty files as the original file contains only 8 lines”.

Running this command in the terminal, you will get the following highlighted output. It will give you only 4 split files and remove the fifth empty file named as xx05.

Option 05: -s, –quiet

  • This option doesn’t display the size count of the output files. The syntax for this command-line option is:
csplit -s Demo.txt 3

In the above command, -s is the command-line option. Demo.txt is the filename as described above in step 02.

Running this command in the terminal, you will get the following highlighted output.

Option 06: -h, –help

The –-help option simply displays the help message and exit. You will write the following command in the terminal:

 csplit --help

The below-attached screenshot is showing the output of the above command on running it in the terminal.

Option 07: -v, –version

The –version option simply displays the version information of the csplit command. You will write the following command in the terminal:

 csplit --version

The below attached is showing the output of the above command on running it in the terminal.

Conclusion

If you are using the csplit command for the first time and have trouble understanding its usage and implementation, this guide will explain everything in detail and walk you through the entire process. The document not only explains each command line option and its syntax with examples, but also demonstrates it with highlighted screenshots for each step of the entire process. The document is very understandable and effective not only for laymen, but also for professionals and IT users.

Linux csplit command explained (with examples)

Karim Buzdar

About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn