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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1528.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="267" src="data:image/svg xml,” width=”795″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1529.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="261" loading="lazy" src="data:image/svg xml,” width=”801″>

Go to the search bar on the top left.

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1530.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="497" loading="lazy" src="data:image/svg xml,” width=”567″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1531.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="378" loading="lazy" src="data:image/svg xml,” width=”799″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1532.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="66" loading="lazy" src="data:image/svg xml,” width=”796″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1533.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="226" loading="lazy" src="data:image/svg xml,” width=”799″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1534.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="219" loading="lazy" src="data:image/svg xml,” width=”420″>

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’.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1535.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="154" loading="lazy" src="data:image/svg xml,” width=”789″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1536.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="153" loading="lazy" src="data:image/svg xml,” width=”578″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1537.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="175" loading="lazy" src="data:image/svg xml,” width=”799″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1538.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="151" loading="lazy" src="data:image/svg xml,” width=”749″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1539.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="37" loading="lazy" src="data:image/svg xml,” width=”544″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1540.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="576" loading="lazy" src="data:image/svg xml,” width=”800″>

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1541.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="183" loading="lazy" src="data:image/svg xml,” width=”799″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1542.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="263" loading="lazy" src="data:image/svg xml,” width=”800″>

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.