Chat Zalo Chat Messenger Phone Number Đăng nhập
Breaking Linux files into pieces with the split command

Breaking Linux files into pieces with the split command

Linux systems provide a very easy to use command to split files into pieces. This is something you may need to do before uploading your files to any storage site that limits the size of the files or emailing them as attachments. To split a file into parts, simply use the split command.

$ split bigfile

By default, the split command uses a very simple naming scheme. The file fragments will be called xaa, xab, xac, etc., and, presumably, if you split a file that’s large enough, you could even get chunks called xza and xzz.

Unless you ask, the command runs without giving you any comment. However, you can use the -verbose option if you want to view file fragments as they are created.

$ split -bigfile verbose creating ‘xaa’ file creating ‘xab’ file creating ‘xac’ file You

can also contribute to file naming by providing a prefix. For example, to name all the parts of your original file bigfile.xaa, bigfile.xab, etc., you would add your prefix to the end of your split command like this: $ split

-verbose bigfile bigfile. Create file ‘bigfile.aa’ create file ‘bigfile.ab’ create file ‘’

Note that a period is added to the end of the prefix shown in the previous command. Otherwise, the files would have names like bigfilexaa instead of bigfile.xaa.

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

$ split -b100M bigfile

File sizes can be specified in kilobytes, megabytes, gigabytes… up to yottabytes! Just use the appropriate letter of K, M, G, T, P, E, Z and Y.

If you want the file to be divided based on the number of lines in each fragment instead of the number of bytes, you can use the -l (lines) option. In this example, each file will have 1,000 lines except, of course, for the last one which may have fewer lines.

$ split -verbose -l1000 logfile log. Create the file ‘log.aa’ Create the file ‘log.ab’ Create the file ‘’ Create the file ‘’ Create the file ‘’ Create the file ‘’ Create the file ‘’ Create the file ‘log.ah’ Create the file ‘’ Create the file ‘log.aj’

If you need to reassemble your file from parts at a remote site, you can do so quite easily using a cat command like one of these:

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

Split and Reassemble with the commands shown above should work for both binary and text files. In this example, we’ve split the zip binary into 50-kilobyte chunks, use cat to reassemble them, and then compare the assembled and original files. The diff command verifies that the files are the same.

$ split -verbose -b50K zip zip. creating file ‘zip.aa’ creating file ‘zip.ab’ creating file ‘’ creating file ‘’ creating file ‘’ $ cat zip.a? > $ diff zip $ <== no way out = no difference

The only caution I have to give at this point is that, if you use split often and use the default name, you will likely end up overwriting some chunks with others and maybe sometimes have more chunks than I expected because some were left over from some previous split.

Contact US