Tarballs and Compression

Tarballs and Compression

TAR (Tape Archive) is used aggregate files and directories into a single file for easily backing up and moving. When unpacked, the files and directories will be completely restored, with all original permissions too! Tarballs are a very easy way to manage large volumes of files, and here I will be creating one to backup my files.


Create a Tarball of the entire home/ directory with the tar command as follows:

skylar@SkyeBook:~$ tar -cvpf tarball.tar /home/

The man tar will show you that tar supports a lot of optional arguments to suit your requirements, however my chosen flags seen here are:

  • -c = compress
  • -v = verbose output
  • -p = preserve file permissions
  • -f = specify file

Moving on, as we see here, we now have a 5.8 GB Tarball in our working directory:

skylar@SkyeBook:~$ ll -lh tarball.tar
-rw-rw-r--  1 skylar skylar 5.8G May 16 21:59 tarball.tar

We can inspect the files in this Tarball once more with -t flag:

tar -tvf tarball.tar

...but there is quite a lot here, and it's none of your business anyway!

Now 5.8GB isn't the end of the world, but in other scenarios you may be faced with finding a way to backup or move much larger Tarballs. Tarballing a production SQL Database or a large file server, for example, would yield an unweidly behemoth of a tarball. Luckily there are some very clever compression algorithms out there to help. Three popular ones are:

  • G-Zip (Native) - A fast and light compression algorthm, but with relateively poor reduction in file size
  • B-Zip 2 (Native) - The stronger compression algorithm, but far more resource intensive to run.
  • 7-Zip, R-Zip and More - Other compression algorithms can outperform the native G-Zip and B-Zip2, for the tradeoff of not being installed natively.

Here we will use the bzip2 compression algorithm to bring the size down a little bit before moving it:

skylar@SkyeBook:~$ bzip2 tarball.tar
skylar@SkyeBook:~$ ll -lh tarball.tar.bz2
-rw-rw-r--  1 skylar skylar 3.6G May 16 22:09 tarball.tar.bz2

Now we can move this tarball where we desire, just like any other file! To extract again, simply decompress and untar:

skylar@SkyeBook:~$ bunzip2 tarball.tar.bz2
skylar@SkyeBook:~$ tar -xvpf tarball.tar

The -x flag here tells tar to extract. You can use -C to specify where to unpack the tarball, otherwise it will unpack in the working directory


Related Article