How to Use the csplit Command to Split Files on Linux

csplit is a popular Linux command line utility used to split the contents of a file into two. The file you need to modify should be a text file with a “.TXT” extension.

The command is easy to use and works well on all Linux distributions. Using the different flags available to csplit, you can also modify the output according to your needs.

Here’s how to use csplit to split a file on Linux.

What is csplit?

Used on Linux and other Unix-like operating systems, csplit can split a file into individual files determined by context lines.

The basic syntax of the command is:


csplit vs split

Most Linux users like to use the split command when trying to split a file into several smaller files. The problem with this command is that it relies on byte size or line size to split the files.

This is not feasible in scenarios where you want to split files based on their content, rather than their size. This is where csplit comes to the rescue, as it splits the file into fixed-size chunks based on content rather than byte count.

How to install csplit on Linux

csplit comes pre-installed on almost all Linux distributions. However, if you are facing “csplit: command not found” error, this means that the tool is not installed on your system. To install csplit on Ubuntu, run:

sudo apt-get install coreutils

On Arch Linux, run:

sudo pacman -S coreutils

To install csplit on Fedora and RHEL:

sudo dnf install coreutils

How to use csplit on Linux

To see how csplit works, create a text file on your system. Use the touch command to create an empty file.

touch filename.txt

Once you have created the file, open it with the nano editor to modify its content.

nano filename.txt

Once you’ve added some content to the file, press Ctrl+X and later Y to save and close it.

To check the contents of the file using the cat command, run:

cat filename
a text file is being created in the ubuntu terminal

Use the csplit command to split a file

To understand how csplit works, first look at the content of the file used here as an example.

A file and its contents are displayed.

The file contains nine lines numbered 1 through 9. If you have to split the file in two, how do you tell csplit which contents to send to the first file and which to the other? That is easy. In the command, you just need to tell csplit which line to start the split from.

This is done by specifying the line number. For example, if you want to split the file from the third line with the word “London”, you will mention 3 in the command. Enter the command like this:

csplit filename.txt 3

This command will instantly split the file in two. Use the ls command to list all the contents of the directory to view the output files. You will find the new files with the names xx00 Y xx01 along with the original file.

Use the cat command to check the contents of both files.

In ubuntu terminal the csplit command has been used to split a file

As you can see, csplit splits the file into two parts from the third line as specified in the command.

csplit Command Options

Here are some of the csplit command line options you can use:

1. Change the prefix of the output files

Also known as the prefix flag, -F modifies the prefix in the file name. You may have noticed that when csplit splits the file, the new files created have XX as a prefix to file names. You can change that using the -F flag in command.

For example, if you want the file names to have to BC as a prefix instead of XXissue the command like this:

csplit -f abc filename.txt 3
The csplit command has been used with the -f flag

As seen, after splitting, both files have to BC as a prefix in names.

2. Save files when errors occur

The -k or the –Keep the files The option does not delete the output files if there is an error in the csplit command.

Issue the following faulty command:

csplit -k randomfile.txt 2 {3}
The csplit command has been used with the k option

3. Modify the number of digits in the file name

With this option, you can tell the csplit command how many digits you want to see in the file name after the prefix. It is also called the digit flag.

Run the following command to keep only one digit in the file name:

csplit -n 1 randomfile.txt 2
The csplit command is being used with the n flag

Without him -north flag, by default, you’ll see two digits in the file name.

4. Split file without generating size count

Also known as the quiet flag, the -s flag silently splits the file without mentioning the size count of the output files.

csplit -s randomfile.txt 3
The csplit command is being used with the s flag

5. View command line help

To see details of all the options available to csplit, use the -h either –help flag in command.

The csplit command is being used with the h flag

6. Check csplit version number

To see what version of csplit you are using, run the command with the –version flag:

The csplit command is being used with the v flag

7. Skip a specific line when splitting

You can also use the –delete-paired command line option to skip a particular line when splitting the file.

csplit --suppress-matched filename.txt 5

When creating the two files, csplit will ignore the fifth line and split the file on the next line.

The csplit command is being used with the matching delete option

The fifth line of the original file has the word “Berlin”. In the output file, “Berlin” was omitted.

Split files effortlessly with a single command on Linux

There are many command line utilities available to manage files on a Linux system. One of them is csplit. By default, it is available on all Linux systems. If not, you can simply install it via the command line.

csplit is an easy and efficient way to split a file when you have to split the file based on its content. csplit comes with several command line options that give you the flexibility to tailor the output the way you want. There are several command line tools to view the contents of a file on Linux if you want to check the files after splitting them.

Leave a Reply

Your email address will not be published. Required fields are marked *