csplit is a popular Linux command-line utility used to split the contents of a file into two. The file you need to alter must be a text file with a ".txt" extension.

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

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 [OPTION] [PATTERN]
    

csplit vs. split

Most Linux users like to use the split command when it comes to splitting a file into multiple smaller files. The issue with this command is that it relies on the byte size or line size to divide the files.

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

How to Install csplit on Linux

csplit comes pre-installed on almost all Linux distributions. However, if you face a “csplit: command not found" error, this means the tool is not installed on your system. To install csplit on Ubuntu, execute:

        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've created the file, open it with the nano editor to modify its content.

        nano filename.txt
    

Once you have added some content to the file, press Ctrl + X and then Y to save and close it.

To verify the file contents using the cat command, run:

        cat filename
    
a text file is being created on Ubuntu terminal

Use the csplit Command to Split a File

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

A file and its contents are displayed

The file contains nine lines from numbers 1 to 9. If you have to divide the file into two, how will you tell csplit which contents to send to the first file and which ones to the other? That’s easy. In the command, you just need to tell csplit from which line to start the split.

This is done by specifying the line number. For instance, 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 divide the file into two. Use the ls command to list down all the directory contents to see the output files. You will find the new files with the names xx00 and xx01 alongside the original file.

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

On ubuntu terminal, csplit command has been used to split a file

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

The csplit Command Options

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

1. Change the Prefix for Output Files

Also known as the prefix flag, -f modifies the prefix in the filename. You might have noticed when csplit splits the file, the new files created have xx as the prefix in the filenames. You can change that by using the -f flag in the command.

For example, if you want the filenames to have abc as a prefix instead of xx, issue the command like this:

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

As visible, after the split, both files have abc as the prefix in the names.

2. Keep the Files When Errors Occur

The -k or the --keep-files option doesn’t remove the output files if there’s an error in the csplit command.

Issue the following faulty command:

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

3. Modify the Number of Digits in the Filename

Using this option, you can tell the csplit command how many digits you want to see in the filename following the prefix. It is also called the digits flag.

Issue the following command to keep only one digit in the filename:

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

Without the -n flag, by default, you will see two digits in the filename.

4. Split the File Without Outputting 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
    
csplit command is being used with s flag

5. View Command-Line Help

To see details of all the options available for csplit, use the -h or --help flag in the command.

        csplit --help
    
csplit command is being used with h flag

6. Check the csplit Version Number

To see which version of csplit you are using, execute the command with the --version flag:

        csplit --version
    
csplit command is being used with v flag

7. Omit a Specific Line While Splitting

You can also use the --suppress-matched command-line option to omit a particular line when splitting the file.

        csplit --suppress-matched filename.txt 5
    

While creating the two files, csplit will ignore the fifth line and split the file from the next line.

csplit command is being used with suppress matched option

The fifth line in 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 divide the file based on its contents. csplit comes with various command-line options that offer you the flexibility to tailor the output the way you want. There are several command-line tools to view the content of a file on Linux if you want to check the files after splitting.