Split file into multiple files using split command line utility on unix / linux

Split Command in Unix / Linux:

This utility / command in unix or linux helps to split the file into two or more files. We have multiple options to use the command as per our needs.

Syntax :

split [filename] [INPUT [PREFIX] ]

where
[filename] is the name of file we want to split.
[INPUT [PREFIX]]  is optional prefix for the splited files.
for example if we have file name called “abc” then usually we will keep Prefix as “abc” so that it can be divided into files are called abc0 , abc1, abc2 … abc[N]

OPTIONAL

-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
 --verbose              print a diagnostic just before each output file is opened

Examples 1 :

Lets create a file which will contain 100 lines using following command

for i in {1..100} ; do echo "This is line number $i ">>test-split.txt; done
bash-4.1$ ls
test-split.txt

Now split the file into 10 files each will contain

split -l 10 test-split.txt test-split.txt -d

Note:  with -d option each file will have digit in suffix as shown below but if the number of files could go beyond 99 or more than 2 diget number we can us -a3 or -aN option for suffix length where N is length of suffix (default is -a2)

therefore file is split into 10 as shown below

bash-4.1$ ls
test-split.txt test-split.txt00 test-split.txt01 test-split.txt02 test-split.txt03 test-split.txt04 
test-split.txt05 test-split.txt06 test-split.txt07 test-split.txt08 test-split.txt09

Example 2.

Lets create a bigger file of 10M and split it with respect to size in MB.

bash-4.1$ ls -ltrh
total 10M  

-rw-r-----. 1 rmn users 10M Jul 20 15:13 test-split.txt

Now we split this 10M file into 10 equal size files of 1M appx using following command.

bash-4.1$ split -b 1M test-split.txt test-split.txt -d

File have been splited into 10 files as show below

bash-4.1$ ls -ltrh
total 20M
-rw-r-----. 1 rmn     users 10M Jul 20 15:13 test-split.txt
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt00
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt01
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt02
-rw-r-----. 1 rmn      users 1.0M Jul 20 15:17 test-split.txt03
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt04
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt05
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt06
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt07
-rw-r-----. 1 rmn     users 1.0M Jul 20 15:17 test-split.txt08
-rw-r-----. 1 rmn     users 927K Jul 20 15:17 test-split.txt09

If you want to split the file in bytes then command will be as followed

bash-4.1$ split -b1000 test-split.txt test-split.txt -d

Finally how to join all back to make one single file ? it is very simple just use cat command line utility as shown belown

bash-4.1$ cat test-split.txt00 test-split.txt01 test-split.txt02 test-split.txt03 test-split.txt04 test-split.txt05 test-split.txt06 test-split.txt07 test-split.txt08 test-split.txt09 >> test-split.txt

 

Thanks for reading article I hope you liked it .. Keep visiting and don’t forget to give feedback

 

Leave a Reply

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