Contact Us

Breaking Linux files into pieces with the split command | Network World

tagsHdmi Port Switch



Unix Dweeb,

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

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

Unless you request it, the command will not give any feedback at runtime. However, if you want to see them when creating file blocks, you can use the --verbose option.

You can also facilitate file naming by providing a prefix. For example, to name all the contents of the original files bigfile.xaa, bigfile.xab, etc., you can add the prefix to the end of the split command as follows:

Note that a dot has been added to the end of the prefix shown in the above command. Otherwise, the file will have a name like bigfilexaa instead of bigfile.xaa.

Please note that the split command does

Delete the original file and only create the block. If you want to specify the size of the file block, you can use the -b option to add it to the command. E.g:

The file size can be specified in kilobytes, megabytes, or gigabytes, up to yobytes! Just use the appropriate letters from 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 instead of the number of bytes, you can use the -l (line) option. In this example, each file will have 1,000 lines, of course, the last file may have fewer lines.

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

Splitting and reassembly using the commands shown above should be applicable to binary files and text files. In this example, we split the zip binary file into 50 KB blocks, reassembled them using cat, and then compared the assembled file with the original file. The diff command verifies that the files are the same.

At this point, the only thing I should pay attention to is that if you use split often and use the default naming, you may end up being overwritten by other blocks, and sometimes there may be more blocks than you expected because some of them are left of. Some earlier splits.

Sandra Henry-Stocker has been engaged in the management of Unix systems for more than 30 years. She describes herself as "USL" (Unix is ​​a second language), but she remembers enough English to write books and buy food. She lives in the mountains of Virginia, where, when she is not using Unix or writing articles about Unix, she is driving the bear away from the bird feeder.

Copyright © 2019