11 Useful Split Command in Linux With Examples

In this blog post, we will cover 11 useful split command in Linux with examples, helping you to become a proficient user.

The Linux terminal is a powerful tool that offers a wide range of commands for manipulating files and data. One such command that comes in handy when dealing with large files is the split command. This command allows you to split files into smaller parts based on various criteria. By default, the split command divides a file into parts of approximately 1,000 lines each. Additionally, the output files are prefixed with the letter ‘x’

Syntax of Split Command

# split {options} {file_name} {prefix}

Options:

Split-Command-Options-Linux

1) Split File into Pieces

Let’s assume we have file name with tuxlap.txt, Use below split command to break into the pieces

[root@linuxtechi ~]# split tuxlap.txt
[root@linuxtechi ~]# ll
total 32
-rw-------. 1 root root  980 Aug 12 00:11 anaconda-ks.cfg
-rw-r--r--. 1 root root 9607 Nov 11 03:22 tuxlap.txt
-rw-r--r--. 1 root root 8744 Nov 11 03:23 xaa
-rw-r--r--. 1 root root  863 Nov 11 03:23 xab
[root@linuxtechi ~]#

As we can see the above output ‘tuxlab.txt‘ is split into two pieces with the name ‘xaa’ and ‘xab’.

Use ‘–verbose‘ option to view verbose output of split command, example is shown below:

[root@linuxtechi ~]# split tuxlap.txt --verbose
creating file ‘xaa’
creating file ‘xab’
[root@linuxtechi ~]#

2) Specifying the Number of Output Files

If you have a specific number of parts in mind, you can use the “-n” option. For instance, split -n 5 largefile.txt part_ will divide “largefile.txt” into five equal-sized parts named “part_aa,” “part_ab,” and so on.

[root@linuxtechi ~]# split -n5 linux-lite.iso
[root@linuxtechi ~]# ll
total 2048124
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rwx------. 1 root root 1048576000 Nov 11 03:54 linux-lite.iso
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
-rw-r--r--. 1 root root  209715200 Nov 11 05:22 xaa
-rw-r--r--. 1 root root  209715200 Nov 11 05:22 xab
-rw-r--r--. 1 root root  209715200 Nov 11 05:22 xac
-rw-r--r--. 1 root root  209715200 Nov 11 05:23 xad
-rw-r--r--. 1 root root  209715200 Nov 11 05:23 xae
[root@linuxtechi ~]#

3) Splitting Based on Line Count

The split command is not limited to file sizes; it can also split files based on the number of lines. To split a file into sections of 200 lines each, use split -l 200 data.txt part_. This feature is handy when dealing with text files, logs, or other structured data.

[root@linuxtechi ~]# split -l200 tuxlap.txt --verbose
creating file ‘xaa’
creating file ‘xab’
creating file ‘xac’
creating file ‘xad’
creating file ‘xae’
creating file ‘xaf’
[root@linuxtechi ~]#

Verify the lines of each file using below command

[root@linuxtechi ~]# wc -l xa*
 200 xaa
 200 xab
 200 xac
 200 xad
 200 xae
  91 xaf
1091 total
[root@linuxtechi ~]#

4) Splitting a File into Equal-Sized Parts

Sometimes, you may have a large file that needs to be divided into smaller, more manageable chunks. Using Split command we can split a large file with equal file size using -b option.

Use the following syntax to split files with size in bytes, KB , MB and GB

# split  -b{bytes}  {file_name}

# split  -b  nK      {file_name}    // n is the numeric value

# split  -b   nM    {file_name}      // n is the numeric value

# split  -b   nG     {file_name}     // n is the numeric value

Split file based on bytes:

[root@linuxtechi ~]# split -b2000000 tuxlap.txt

Split file based on KB:

[root@linuxtechi ~]# split -b 50K tuxlap.txt

Split file based on MB:

[root@linuxtechi ~]# split -b 50M tuxlap.txt

Split file based on GB:

[root@linuxtechi ~]# split -b 1G tuxlap.txt

5) Split Files with Numeric Suffix Instead of Alphabetic

In above discussed examples, we have seen that split command output files are created with alphabetic suffix like xaa, xab….. xan , Using  ‘-d‘ option with split command to create split output files with numeric suffix like x00, x01, … x0n

[root@linuxtechi ~]# split -d tuxlap.txt

[root@linuxtechi ~]# ll
total 1024256
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rwx------. 1 root root 1048576000 Nov 11 03:54 linux-lite.iso
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
-rw-r--r--. 1 root root      11998 Nov 11 04:41 x00
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x01
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x02
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x03
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x04
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x05
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x06
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x07
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x08
-rw-r--r--. 1 root root      12000 Nov 11 04:41 x09
-rw-r--r--. 1 root root         12 Nov 11 04:41 x10
[root@linuxtechi ~]#

6) Spliting File with Customize Suffix

With split command we can create output files with customize suffix. Let’s assume we want to create split output files with customize suffix

Syntax:

# split  {file_name}  {prefix_name}

[root@linuxtechi ~]# split tuxlap.txt split_file_

[root@linuxtechi ~]# ll
total 1024248
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rwx------. 1 root root 1048576000 Nov 11 03:54 linux-lite.iso
-rw-r--r--. 1 root root      11998 Nov 11 04:56 split_file_aa
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ab
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ac
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ad
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ae
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_af
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ag
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ah
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_ai
-rw-r--r--. 1 root root      12000 Nov 11 04:56 split_file_aj
-rw-r--r--. 1 root root         12 Nov 11 04:56 split_file_ak
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
[root@linuxtechi ~]#

When working with CSV files, it can be beneficial to split them into smaller parts for processing or analysis. Use

# split -l 1000 data.csv part_

This command will split a CSV file into parts containing 1000 lines each.

7) Splitting and Zipping

To save disk space, you can combine splitting and zipping in a single step. For instance,

# split -b 1M largefile.txt -d -a 3 - | gzip > largefile.zip

Above command will split the file and create a compressed zip archive containing the parts.

8) Prevent Zero Size Split output files

There can be some scenarios where we split a small file into a large number of chunk files and zero size split output files can be created, so to avoid zero size split output file, use the option ‘-e’

[root@linuxtechi ~]# split -n60 -e tuxlap.txt
[root@linuxtechi ~]# ls -l x*
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xaa
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xab
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xac
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xad
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xae
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xaf
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xag
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xah
.............
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xce
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xcf
-rw-r--r--. 1 root root 2000 Nov 11 05:34 xcg
-rw-r--r--. 1 root root 2010 Nov 11 05:34 xch
[root@linuxtechi ~]#

9) Create Output files of Customize Suffix Length

Let’s suppose we want to split an ISO file and where size of each output file is 500MB and suffix length is to be 3.  Use ‘-a’ option in split command, example is shown below,

[root@linuxtechi ~]# split -b 500M linux-lite.iso -a 3

[root@linuxtechi ~]# ll
total 2048124
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rwx------. 1 root root 1048576000 Nov 11 03:54 linux-lite.iso
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
-rw-r--r--. 1 root root  524288000 Nov 11 05:43 xaaa
-rw-r--r--. 1 root root  524288000 Nov 11 05:43 xaab
[root@linuxtechi ~]#

10) Spliting and Merging ISO File

Let’s suppose we have a Windows Server ISO file of size 4.2 GB and we are unable to scp this file to remote server because of its size.

To resolve such type of issues, we can split the ISO into n number of pieces and will copy these pieces to remote sever and on the remote server we can merge these pieces into a single file using cat command,

[root@linuxtechi ~]# split -b 800M Windows2012r2.iso Split_IS0_

View the split output files using ll command,

[root@linuxtechi ~]# ll
total 8871788
-rw-------. 1 root root        980 Aug 12 00:11 anaconda-ks.cfg
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_aa
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_ab
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_ac
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_ad
-rw-r--r--. 1 root root  838860800 Nov 11 06:29 Split_IS0_ae
-rw-r--r--. 1 root root  347987968 Nov 11 06:29 Split_IS0_af
-rw-r--r--. 1 root root     120010 Nov 11 04:39 tuxlap.txt
-rwx------. 1 root root 4542291968 Nov 11 06:03 Windows2012r2.iso
[root@linuxtechi ~]#

Now scp these files to remote server and merge these files into a single using cat command

[root@linuxtechi ~]# cat Split_IS0_a* > Windows_Server.iso
[root@linuxtechi ~]#

11) Verify the Integrity of Merged File via md5sum Utility

As per Example 10, once the split output files are merged into a single file, then we can check the integrity of actual & merge file with md5sum utility. Example is shown below:

[root@linuxtechi ~]# md5sum Windows2012r2.iso
5b5e08c490ad16b59b1d9fab0def883a  Windows2012r2.iso
[root@linuxtechi ~]#

[root@linuxtechi ~]# md5sum Windows_Server.iso
5b5e08c490ad16b59b1d9fab0def883a  Windows_Server.iso
[root@linuxtechi ~]#

Output above confirms that integrity is maintained and we can also say split file are successfully merged into a single file.

Conclusion

The split command in Linux provides a wealth of options for efficiently dividing files into smaller parts. Whether you need to split files by size, lines, or customize output filenames, split has you covered. By exploring the eleven examples discussed in this blog post, you can now leverage the full potential of the split command to improve your productivity, simplify data management, and optimize your Linux experience. Don’t forget to share your valuable feedback and comments in below comments section.

Read Also : 26 Useful find Command Examples in Linux

Read Also : 16 Echo Command Examples in Linux

7 thoughts on “11 Useful Split Command in Linux With Examples”

  1. While split on file the output files are with is extension.
    Like:
    split -l300 -b abc.json

    Needed output like :
    abc00.json
    abc01.json
    abc03.json

    How can i get the splited files with its extension?

  2. Brice Coustillas

    Please note that in Darwin (a Macintosh variant of BSD Unix), M is not accepted as a size suffix — it has to be m (lowercase); also G or g is not accepted while thousands of megabytes are.

  3. Thanks for the bit about using cat to reassemble the original file.
    I could not find that anywhere else.
    It allowed me to get the job done.

Leave a Comment

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