UNIX / Linux split Command Examples

What is split?
5 split examples
Syntax and Options
Related Commands

What is split?

split command splits a single file into multiple files.


5 split Examples

Split the file into multiple pieces with 1000 lines each

Split command splits the file into 1000 lines per file, and names the files as PREFIXaa, PREFIXab, PREFIXac, and so on. By default the PREFIX is x, and the number of lines is 1000 lines per file.

$ split mylog
$ wc -l *
     4450 mylog
     1000 xaa
     1000 xab
     1000 xac
     1000 xad
      450 xae

Split the file into 20 MB pieces

Split the file into 20 MB files as shown below with -b option.


$ split -b 20M logdata
$ ls -lh | tail -n +2
-rw------- 1 sathiya sathiya 102M Jul 25 18:47 logdata
-rw------- 1 sathiya sathiya  20M Jul 25 19:20 xaa
-rw------- 1 sathiya sathiya  20M Jul 25 19:20 xab
-rw------- 1 sathiya sathiya  20M Jul 25 19:20 xac
-rw------- 1 sathiya sathiya  20M Jul 25 19:20 xad
-rw------- 1 sathiya sathiya  20M Jul 25 19:20 xae
-rw------- 1 sathiya sathiya 1.6M Jul 25 19:20 xaf

Split the file into 50 MB pieces with the given prefix

Split the file into 50 MB files as shown below with –bytes option which is similar as the -b option, and give the prefix as second argument.

$ split --bytes=50M logdata mydatafile
$ ls -lh
total 204M
-rw------- 1 sathiya sathiya 102M Jul 25 18:47 logdata
-rw------- 1 sathiya sathiya  50M Jul 25 19:23 mydatafileaa
-rw------- 1 sathiya sathiya  50M Jul 25 19:23 mydatafileab
-rw------- 1 sathiya sathiya 1.6M Jul 25 19:23 mydatafileac

Split the file based up on the number of lines

Split the file into multiple pieces based up on the number of lines using -l option as shown below.

$ wc -l testfile
2591 testfile
$ split -l 1500 testfile importantlog
$ wc -l *
  1500 importantlogaa
  1091 importantlogab
  2591 testfile

Split the file and name it with numbers

Use -d option to name the files with number suffixes as 00, 01, 02 .. and so on, instead of aa, ab, ac.

$ split -d testfile
$ ls
testfile  x00  x01  x02
Short Option Long Option Option Description
-a –suffix-length=N use suffixes of length N (default 2)
-b –bytes=SIZE put SIZE bytes per output file
-C –line-bytes=SIZE put at most SIZE bytes of lines per output file
-d –numeric-suffixes use numeric suffixes instead of alphabetic
-l –lines=NUMBER put NUMBER lines per output file

Related Commands

cut

Comments on this entry are closed.

  • gowri June 3, 2012, 9:53 pm

    I easily understand this.

  • sarah August 20, 2012, 1:21 am

    Very useful – simple commands, easily understood and applied. Thanks!

  • Rajkumar K January 29, 2013, 3:35 am

    Good Sample code

  • sridhar April 7, 2013, 5:22 am

    thanks for the command shared, very useful.

  • Dhirendra July 24, 2013, 12:17 am

    Thanks,
    To explain the command with proper example

  • Hi August 6, 2013, 11:22 pm

    Hi,
    I have one file ,In this files i am having 1000 records ,file name calles data.txt ,

    i want to split 1000 records to 5 different files ,In each file 2000 records .(through the unix command)

  • Sandeep August 29, 2013, 10:59 am

    Hi All,

    I couldn’t and i’m sorry for that, I have a file which has 27000 lines and i would like to split it as 1000 lines, the file name is test1. Can i have the command

    Thanks,
    Sandeep

  • tanuja kholia October 19, 2013, 5:45 am

    Now i can easily understand split command. Examples are more effective.

  • innn December 26, 2013, 4:56 am

    is it possible to use it to split not with numeric suffixes but prefixes. such as:
    01foobar.mp3
    02foobar.mp3
    03foobar.mp3
    .
    .
    .?