This article describes how to split large files in linux. for example, a 5 GB log file is cut into many small pieces. For more information, see
To split a large file in linux, for example, a 5 GB log file, you need to divide it into multiple small files to facilitate reading by a common text editor.
Sometimes, if you want to transfer a 20 GB large file to another server, you also need to split it into multiple files to facilitate data transmission.
The following uses five different examples to explain how to split large files in Linux for your reference.
Example 1: split the file by 1000 lines
The split command splits the file into 1000 lines for each file, and the file names are in turn [prefix] aa, [prefix] AB, [prefix] ac, etc. the default prefix is X, the number of lines in each file is 1000.
Command:
Copy codeThe code is as follows:
$ Split mylog-l 1000
$ Wc-l *
4450 mylog
1000 xaa
1000 xab
1000 xac
1000 xad
450 xae
Example 2: 20 MB for each file
Split the file into multiple 20 MB files with the-B option.
Command:
Copy codeThe code is as follows:
$ Split-B 20 M logdata
$ Ls-lh | tail-n + 2
-Rw ------- 1 sathiya 102 M Jul 25 logdata
-Rw ------- 1 sathiya 20 M Jul 25 19:20 xaa
-Rw ------- 1 sathiya 20 M Jul 25 19:20 xab
-Rw ------- 1 sathiya 20 M Jul 25 19:20 xac
-Rw ------- 1 sathiya 20 M Jul 25 19:20 xad
-Rw ------- 1 sathiya 20 M Jul 25 19:20 xae
-Rw ------- 1 sathiya 1.6 M Jul 25 xaf
Example 3: 50 MB prefix for each file
Use the-bytes option to split the file into multiple 50 MB files.-bytes is similar to the-B option and specifies the prefix in the second parameter.
Command:
Copy codeThe code is as follows:
$ Split -- bytes = 50 M logdata mydatafile
$ Ls-lh
Total 204 M
-Rw ------- 1 sathiya 102 M Jul 25 logdata
-Rw ------- 1 sathiya 50 M Jul 25 19:23 mydatafileaa
-Rw ------- 1 sathiya 50 M Jul 25 19:23 mydatafileab
-Rw ------- 1 sathiya 1.6 M Jul 25 mydatafileac
Example 4. split a file based on the number of lines
Use the-l option to specify the number of lines to split the file into multiple files with the same number of lines.
Command:
Copy codeThe code is as follows:
$ Wc-l testfile
2591 testfile
$ Split-l 1500 testfile importantlog
$ Wc-l *
1500 importantlogaa
1091 importantlogab
2591 testfile
Example 5. split a file with a numeric suffix
You can use the-d option to specify the suffix as a number, for example, 02..., instead of aa, AB, and ac.
Command:
Copy codeThe code is as follows:
$ Split-d testfile
$ Ls
Testfile x00 x01 x02
Available options
Copy codeThe code is as follows:
Description
-B-bytes = SIZE the SIZE of each output file, in bytes.
-C-line-bytes = SIZE: maximum number of bytes in a single row in each output file.
-D-numeric-suffixes uses numbers as suffixes.
-L-lines = NUMBER indicates the NUMBER of columns in each output file.
Description
-B-bytes = SIZE the SIZE of each output file, in bytes.
-C-line-bytes = SIZE: maximum number of bytes in a single row in each output file.
-D-numeric-suffixes uses numbers as suffixes.
-L-lines = NUMBER indicates the NUMBER of columns in each output file.