Tar archives can be split into multiple archives of a certain size, which is handy if you need to put a lot of content onto discs. It’s also useful if you have a huge archive that you need to upload, but would rather do it in chunks. In this guide, we’ll show you the commands you need in order to split tar archives into multiple blocks on a Linux system.
This will work regardless of what type of compression (or lack thereof) that you use. So files with extensions like .tar
, tar.gz
, tar.xz
, etc. can all be split into chunks. We’ll also show you how to extract files from archives that have been split into numerous files.
In this tutorial you will learn:
- How to split tar archives into multiple files
- How to open split tar archives
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | tar, split, and optional compression |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Split tar archives into multiple blocks
To split tar archives into multiple files, we’ll pipe our tar
command over to split
. Let’s look at an example.
This command will split a gzip compressed tar archive into 5MB chunks:
$ tar cvzf - example-dir/ | split --bytes=5MB - myfiles.tar.gz.
In our example, we split three large text files into 5MB tar archives. As you can see, the files end up with names like:
$ ls myfiles* myfiles.tar.gz.aa myfiles.tar.gz.ac myfiles.tar.gz.ae myfiles.tar.gz.ag myfiles.tar.gz.ab myfiles.tar.gz.ad myfiles.tar.gz.af
You can use any options in your tar
command that you’d like. For example, using bzip2 compression instead of gzip. What really matters is that you also include the -
option, which sends tar output to stdout. The split
utility can then interpret that data and split it into multiple files of a specific size.
If you need to split your archives into some other size, simply specify the proper size after the --bytes=
option in the split
command.
Open split tar archives
To open the split tar archive that we’ve created, you can use the cat
command, piped to the tar
command.
$ cat myfiles.tar.gz.* | tar xzvf -
The options you use with tar
should be what you’d ordinarily use to extract the archive, along with the -
option. In our case, we’re extracting a tar archive that has been compressed with gzip, so we use xzvf
.
Closing Thoughts
In this guide, we saw how to make tar archives on Linux, and have them split into multiple blocks of a certain size. The tar
and split
commands prove perfect for the job. It’s a recurring theme on Linux for two or more commands to be strung together in order to achieve a single goal, and this is a perfect example of that.