Have you been looking for a solution to split binary files on Linux OS? We’ve got a detailed guide for you right here.
Linux OS comes with a user-friendly command for splitting up files. You might need to do this before sending your files as attachments through email or uploading them to a storage service with file size restrictions. We’re here to help you divide a file into sections using the split command.
When can the Split Command be used for?
Quite often, you want to break a large file into smaller chunks. The reason to split a file is portability and ease of transmission from one system to another. For instance, a large file requires much time to send over the network. In addition, there are often limitations of the software or email on the size of the file.
This article will discuss how you can split a binary file into smaller ones in Linux. Binary files store data efficiently in compressed form. This may include audio, video, programs, or software data. Before transmitting a large file, you will split the file into different small files that can be combined at the receiving end.
General usage of the Split Command
The general format to split a binary file is:
$split [options] file.bin prefix
Where the file is the name of the file to be split. Replace it with the file’s name that needs to be broken down. A prefix is provided if you want to supply it to the chunk’s name. There are two most widely used options you can use with the split command:
l line number
(i.e., each of the split files has this much number of lines)b bytes
(i.e., the size of the split files)
When the file is split, the output files are named as xa
, xb
, xc
, and etc. By default, the split uses a general naming scheme. If there are multiple splits, that file may be named as xza
, xzb
, xzc
, and so on.
Split a file based on numeric prefix
By default, split works by providing an alphabetical prefix (i.e. xa
, xb
) for file names. You can also use a numerical prefix. For this purpose, you can use the -d
option. The following command splits the file by numerical prefix.
Input:
$ split -d file.bin
Using the –verbose option
By default, the split command works silently and shows no output. If you want to see the progress and the file names during the split, you can use the --verbose
option. The following command splits the file using the verbose option.
Input:
$split --verbose file.bin
The command will then split the file and show the file’s name after the split.
Using the –prefix option
You can use the prefix option to provide an attachment to the name of the file that is split. The prefix is provided after the name of the file. For instance, the following command provides a prefix (prefix.) for prefixing the file name’
Input:
$split --verbose file.bin prefix.
Note that a ‘.’ is also appended to the file’s name. Furthermore, the split command doesn’t delete the original file but creates several chunk files.
Specifying the size of the split file
You can also specify the size of each split file while splitting them. Its size can be specified in kilobytes and megabytes to yottabytes identified by letters from K to Y (K for kilobytes, M for megabytes, G for gigabytes).
The following command shows an example that splits the file in 512 KBs:
$split -b512K file.bin
Splitting based on the number of lines
In addition, you can split the file based on the number of lines instead of the size of each chunk. For this purpose, you will use –l
option specifying the number of lines. The following command split binary files on Linux into different chunks, each with 500 lines:
$split -l 1000 file.bin
Note that by default, Linux splits the file into chunks of 1000 lines.
Reassembling file after split
Once the file is split, and chunks are transmitted over the network or ported to another computer, one can reassemble them into a single file using the cat command. The following command merges all the chunks into a single file.
Input:
$ cat x?? > merge.bin
You can use the diff command to verify that both files are identical. The following command checks if the two files are the same.
Input:
$diff file.bin merge.bin
In addition, you can use md5 commands to verify the file’s integrity.
Compressing the file before split
Additionally, you can compress the original file before splitting it to reduce the number of chunks. The following command performs compression before splitting.
Input:
$ gzip < file.bin| split -b 32M file.bin.gz
Useful Tips
- If you use the split command often and use the default naming scheme, it is possible that you will overwrite some of the chunks.
- Sometimes, you may even get more chunks than expected.
This article discusses Linux’s split command that can be used to break large files into smaller ones. We also talked about how split files can be combined using different commands. Along with how you can specify their prefixes, and customize the size of each individual file based on your preferences.
If you want to know more information about how to split binary files on Linux, see the manual by typing: man split.
If this guide helped you, please share it.