Bootstrap FreeKB - Linux Commands - split (divide a single file into multiple files)
Linux Commands - split (divide a single file into multiple files)

Updated:   |  Linux Commands articles

The split command splits a single file into multiple files. For example, let's say example.txt has 10 lines of text.

Line 01
Line 02
Line 03
Line 04
Line 05
Line 06
Line 07
Line 08
Line 09
Line 10

 

example.txt can be split into two files, where each file contains 5 lines of text.

  • -l5 = Each new file will contain 5 lines from the target file
  • example.txt = target file
  • new = Each new file will begin with the word "new"
~]# split -l5 example.txt new

 

After running this command, there will be three files, example.txt and the two new files (newaa and newab).

example.txt
newaa
newab

 

newaa will contain lines one through five, and newab will contain lines six through ten.

~]# cat newaa
Line 01
Line 02
Line 03
Line 04
Line 05

~]# cat newab
Line 06
Line 07
Line 08
Line 09
Line 10

 

Split base on size of file

Let's say large.txt is 500 MB.

~]# ls -lh
-rw-r----- 1 root root 500M Jan 1 00:01 large.txt

 

The -b option can be used to split large.txt into five files, where each file is 100 MB.

~]# split -b 100M large.txt small-

 

There will now be five files, each 100 MB.

~]# ls -lh
-rw-r----- 1 root root 500M Jan 1 00:01 log-file
-rw-r----- 1 root root 100M Jan 1 00:01 small-aa
-rw-r----- 1 root root 100M Jan 1 00:01 small-ab
-rw-r----- 1 root root 100M Jan 1 00:01 small-ac
-rw-r----- 1 root root 100M Jan 1 00:01 small-ad
-rw-r----- 1 root root 100M Jan 1 00:01 small-ae

 

If we want the new files to increment numerically instead of alphabetically, we can use the -d option.

~]# split -b 100M -d log-file small-log-

 

Let's see the new results.

~]# ls -lh
-rw-r----- 1 root root 500M Jan 1 00:01 log-file
-rw-r----- 1 root root 100M Jan 1 00:01 small-log-01
-rw-r----- 1 root root 100M Jan 1 00:01 small-log-02
-rw-r----- 1 root root 100M Jan 1 00:01 small-log-03
-rw-r----- 1 root root 100M Jan 1 00:01 small-log-04
-rw-r----- 1 root root 100M Jan 1 00:01 small-log-05

 





Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 21e34c in the box below so that we can be sure you are a human.