Split Command in Linux with Examples

Split command in Linux is used to split large files into smaller files. It splits the files into 1000 lines per file(by default) and even allows users to change the number of lines as per requirement.

The names of the files are PREFIXaa, PREFIXab, PREFIXac, and so on. By default the PREFIX of files name is x and the default size of each split file is 1000 lines per file and both the parameters can be changed with ease. It is generally used with log and archive files as they are very large and have a lot of lines, So in order to break them into small files for analysis split command is used.

Syntax:

split [options] name_of_file prefix_for_new_files

Working with Split Command

1. Split file into short files. Assume a file name with name index.txt. Use below split command to break it into pieces.

split index.txt

Split-file-into-short-files



Index.txt file is split into two pieces with name ‘xaa’ and ‘xab’. It will have 1000 lines in each file by default. The name of split commands is ‘xaa’ and ‘xab’ as we have not set any prefix value.

2. Split file based on number of lines.

split -l 4 index.txt split_file

splitting the file based on number of lines

Index.txt file is split into short files based on the number of lines which we want using -l option as shown.

3. Split command with verbose option. We can also run split command in verbose mode by using ‘–verbose’. It will give a diagnostic message each time a new split file is created.

split index.txt -l 4 --verbose

Split-command-with-verbose-option

Here, we have created a file with name index.txt which will be split into short files and verbose will give us the details of what are the tasks performed.

Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.



4. Split file size using ‘-b’ option.

split -b 16 index.txt index

Split-file-size-using-bytes-option

Here, it will split the file index.txt into separate files called indexaa, indexab, …..with each file containing 16 bytes of data in it.

5. Change in suffix length. By default, the suffix length is 2. We can also change it using ‘-a’ option.

split -l 4 -a 4 index.txt

Change-in-suffix-length

In this it has suffix length 4 on the split files.

Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.

6. Split files created with numeric suffix. In general, the output has a format of x** where ** are alphabets. We can change the split files suffix to numeric by using the ‘-d’ option.

split -l 4 -d index.txt

Split-files-created-with-numeric-suffix

Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.



7. Create n chunks output files. If we want to split a file into three chunk output files then use the ‘-n’ option with the split command which limits the number of split output files.

 split -n 3 index.txt

Create-n-chunks-output-files

It will create three chunks of split files.

8. Split file with customize suffix. With this command, we can create split output files with customizing suffix. Assume, if we want to create split output files with index suffix, execute the following command.

split -l 4 index.txt split_index_

Split-file-with-customize-suffix

Split output files with index suffix will be created.

Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.

9. Avoid zero-sized split files. There are situations when we split a small file into a large number of chunk files and this may lead to zero size split output files. They do not add any value so to avoid it we use the option ‘-e’.

split -l 4 -e index.txt

Avoid-zero-sized-split-files

By using this no zero size split output files will be created.

Note: Here -l 4 is not necessary to use. It is used just for understanding purposes.

10. Split the file into two files of equal length. To split a file equally into two files, we use the ‘-n’ option. By specifying ‘-n 2’ the file is split equally into two files.

split -n 2 index.txt

Split-file-into-two-files-of-equal-length

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.