Detailed explanation of using split command to divide Linux file

  • 2021-07-18 09:21:19
  • OfStack

1 Simple Linux commands allow you to split and reassemble files as needed to accommodate storage or email attachment size restrictions.

The Linux system provides a very easy-to-use command to split files. You may need to do this before uploading a file to a limited-size storage site or as an email attachment. To split the file into multiple file blocks, just use the split command.

$ split bigfile

By default, the split command uses a very simple naming scheme. File blocks will be named xaa, xab, xac, etc. And, roughly, if you split a large enough file, you may even get blocks named xza and xzz.

Unless you ask, this command will run without any feedback. However, if you want to see feedback when creating a file block, you can use the-verbose option.


$ split  In fact, in fact, the -verbose bigfile
creating file 'xaa'
creating file 'xab'
creating file 'xac'

You can also name files with prefixes. For example, to split your original file and name it bigfile. aa, bigfile. ab, etc., you can add a prefix to the end of the split command, as follows:


$ split  In fact, in fact, the -verbose bigfile bigfile.
creating file 'bigfile.aa'
creating file 'bigfile.ab'
creating file 'bigfile.ac'

Note that 1 dot is added to the end of the prefix shown in the above command. Otherwise, the file will be named something like bigfileaa instead of bigfile. aa.

Please note that the split command does not delete your original file, but only creates a file block. If you want to specify the size of the file block, you can add it to the command using the-b option. For example:

$ split -b100M bigfile

The file size can be KB, MB, GB, and the maximum can be YB! Just make the appropriate letters K, M, G, T, P, E, Z and Y.

If you want to split the file based on the number of lines in each block rather than the number of bytes, you can use the-l (line) option. In this example, each file will have 1000 lines, although the last 1 file may have fewer lines.


$ split --verbose -l1000 logfile log.
creating file 'log.aa'
creating file 'log.ab'
creating file 'log.ac'
creating file 'log.ad'
creating file 'log.ae'
creating file 'log.af'
creating file 'log.ag'
creating file 'log.ah'
creating file 'log.ai'
creating file 'log.aj'

If you need to reassemble files on a remote site, you can easily do so using the cat command as shown below:


$ cat x?? > original.file
$ cat log.?? > original.file

The split and combine commands shown above are suitable for binary and text files. In this example, we split the zip binary file into 50KB blocks, then recombined them using cat, and then compared the combined file with the original file. The diff command verifies that the files are identical.


$ split --verbose -b50K zip zip.
creating file 'zip.aa'
creating file 'zip.ab'
creating file 'zip.ac'
creating file 'zip.ad'
creating file 'zip.ae'
$ cat zip.a? > zip.new
$ diff zip zip.new
$          <==  No output  =  No difference 

The only thing I should remind you of is that if you use split frequently and use the default naming, some file blocks may overwrite other file blocks, even more than you expected, because some of them were split earlier.

Summarize


Related articles: