Sun. Jun 7th, 2020

How to use split Linux command to split a file into pieces

1 min read

Have you ever wanted to split a large file into multiple smaller files? For example, a 10Gb log file, we need to divide it into multiple small files, so that we can read it using a normal text editor. Sometimes we need to transfer a large file of 20Gb to another server, which requires us to split it into multiple files to facilitate data transfer. Let us how to use split Linux command to split a file into pieces.

Use

Example

1. Split 1000 lines per file
The split command splits files into 1000 lines per file, and the file names are [prefix]aa, [prefix]ab, [prefix]ac, etc., the default prefix is ​​a, and the number of lines in each file is 1000.

2. Split 20Mb per file

Split the file into multiple 20MB files with the -b option command as follows:

split -b 20M dpkg.log

3. Split 50MB per file with a specified prefix for each file

Use the –bytes option to split the file into multiple 50MB files. The –bytes option is similar to the -b option. Specify the prefix in the second parameter.

split –bytes=50M dpkg.log dpkg_attack

4. Split file based on 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.