Linux Commands

How to Use Split Command in Linux

In Linux, you can split the large files into smaller ones by using a command called split. By default, this command splits the file into 1000 lines per file but you can also split files according to your requirements. By default, the files are split into smaller files and their names start from the prefixes x and size is 1000 lines and you can also change these parameters.

Read this article to know how you can use the split command.

Linux Split Command Syntax

The basic syntax for the split command is given as follow:

split [options] [file] [prefix]

Linux Split Command Options

You have different options while using split command, you can use these options to perform different operations:

Option/Flag Description
-a Set suffix length.
-b Identify size per output file.
-C Maximum size of the file can be determined.
-n Generates a specific number of output files.
-e Omits creating empty output files.
-l Creates files with a specific output line.
-d Change suffixes into numeric values.
–verbose Displays a detailed output.

To split a file into smaller files, use the command syntax given below:

split file_name

For demonstration I have used the above syntax to split the file example.txt into smaller files:

split example.txt

By executing the below command, you can check the smaller files into which the file s converted:

ls

Note: By default, the split command uses the “x” prefix to name the splitted files.

Run the command given below to get the number of lines per file and you can see it is 1000 by default:

wc -l example.txt xa*

Now split a smaller file into the files via the following command given below:

split example2.txt

Run the command given below to check the smaller files created for file example2.txt.

ls

Run the command given below to get the number of lines per file and you can see it is 1000 by default:

wc -l example2.txt xa*

Set Number of Lines per File

Use the -l command with split to override the default 1000-line restriction. split -l command is used to adjust lines number in the file.

For example, I have split a file into smaller files by setting the lines per file equal to 2500:

split -l2500 example.txt

Run the below command to check the number of lines per file:

wc -l example.txt xa*

Run the command given below to split the text into 500-line files:

split -l500 example2.txt

Run the below command to check the number of lines per file that you have set:

wc -l example2.txt xa*

Choose File Size

You can split files based on their size using command split -b. For example, to create 1500 kb file using the file example1.txt run the command given below:

split -b1500K example1.txt --verbose

Run the command given below to check the file size:

wc -c example1.txt xa*

Specify Maximum Size

You can also specify the maximum file size using the split command:

To specify a maximum output file size, use the -C command. For illustration, split example1.txt and provide a 2MB output size by using:

split example1.txt -C 2MB

Set Number of Output Files

Use the -n option to set the numbers of output of your file. For instance, divide example.txt into 10 sections by running the following command:

split example1.txt -n 10

Split a File at the End of a Line

Another way to use the -n option is splitting a file at the end of a whole line.

To do this, combine -n and l. For instance, divide the large text file into 10 files, each of which must conclude with the following entire line:

split -n l/10 example1.txt

Set Suffix Length

You can generate files with a two-letter default suffix using the split command. The -a flag with the split command is use to change the length. For example, to make the suffix three characters long run the command given below:

split -a 3 example1.txt

For further help, use the man command to open the split command manual on the terminal.

man split

Conclusion

This article focused on using split commands in Linux systems. By default, the split command divides a file into 1000-line pieces, each of which is divided into multiple files. You can use the split command to divide large files into smaller files. The above instruction shows you how to divide the files based on specific features using several split commands in Linux.

About the author

Rafia Amjad

I have a degree in Electronics and love to write. My research and writing emphasize on the most recent innovations in gaming and technology.