Avoiding Tarball extraction overkill with 2 simple tricks

Intro

Hello readers

It’s been a while: I’ve been busy at work, going on vacation and been lazy when at home. It’s more than about time to get into my blogging routine again.

This one is actually a short one on Tarball archives. You know, the (compressed) archiving you use to pack your files with. For people unaware of this technology: you can compare it to ZIP and RAR archives but with a little more punch.

Basic usage

Everyone who’s used to working with Linux or other Unix-like systems knows how to create and extract a Tarball archive. But for those who are new to it, here’s an example

Creating an archive

The simplest way to create a Tarball archive is with the following command:

tar cvf myTarArchive.tar folderToArchive
  • Tar: the command used to archive a specified folder
  • Options:
    • C: create an archive
    • V: verbose
    • F: filename to use
  • myTarArchive.tar: the name of the new tar archive
  • folderToArchive: the folder that will be archived and packed into the myTarArchive.rar file

In most cases you will want your Tarball archive to be compressed. The tar command allows GZIP compression by adding the Z option:

tar cvzf myTarArchive.tar.gz folderToArchive
  • Tar: the command used to archive a specified folder
  • Options:
    • C: create an archive
    • V: verbose
    • Z: compress using GZIP compression algorithm
    • F: filename to use
  • myTarArchive.tar.gz: the name of the new tar archive (with the GZ suffix)
  • folderToArchive: the folder that will be archived and packed into the myTarArchive.rar file

Extracting an archive

The exact purpose of this article is facilitating extraction and before we can get into that, I need to show you how basic Tarball extraction works:

tar xvf myTarArchive.tar
  • Tar: the command used to unpack a specified tar archive
  • Options:
    • X: extract an archive
    • V: verbose
    • F: filename to use
  • myTarArchive.tar: the name of the tar archive to extract

If your Tarball archive was compressed, you will need to specify the Z option as well:

tar xvzf myTarArchive.tar.gz
  • Tar: the command used to unpack a specified tar archive
  • Options:
    • X: extract an archive
    • V: verbose
    • Z: apply GZIP decompression
    • F: filename to use
  • myTarArchive.tar.gz: the name of the tar archive to extract (with GZ suffix)

The tricks

Without further ado, I will immediately continue with the actual tricks. Let’s say you have an automatic backup schedule of some crucial folders on your server. A cronjob launches a tar command that archives a specified folder every night.

Extracting a single branch

The reason one backups his files, is to eventually recover lost/corrupt data in case of emergency situations. If that archive happens to be a couple of giga’s, your server will require a lot of computing power to decompress and unarchive the data you want. In a number of cases you don’t want to recover your entire archive since you only a single subfolder.

In that case it would be nice to only extract a single branch of your entire folder tree. For all you GUI users out there, this is peanuts, you just drag and drop. On the command line, it requires an extra argument.

Take for example this folder tree:

  • folder_1
    • subfolder_1
      • subfolder_1_1
      • subfolder 1_2
    • subfolder_2
  • folder 2

Let’s say you only want to extract ‘’subfolder_1_2″. It would be overkill to extract the entire archive, wasting disk, resources and most important: your time. To extract this single branch, you can issue the following statement

tar xvzf myTarArchive.tar.gz folder_1/subfolder_1/subfolder_1_2

What will happen is that in the current working directory a number a folder tree will be created containing only the files of ‘’subfolder_1_2″. Of course it will reside in the hierarchy of the folder tree.

Extracting to a different folder

If can be quite annoying if you want to extract an archive containing files that already exist in your current working directory. These files will be overwritten and you don’t always want to create dummy folders where you can extract you archive. Sometimes you just want extract a file, a folder or an entire branch to a specified folder. The -C option does all that:

tar -C /path/to/selected/folder -xvzf myTarArchive.tar.gz

So this on will extract the myTarArchive.tar.gz archive in the path specified after the -C option.

Combining both tricks

Needless to say you can combine both tricks:

tar -C /path/to/selected/folder -xvzf myTarArchive.tar.gz folder1/subfolder_1/subfolder_1_2

One Response to “Avoiding Tarball extraction overkill with 2 simple tricks”

  • coudenysj Says:

    The “Extracting a single branch” looks very useful for our backup procedure!

    One little remark: The -f parameter is there to specify the name (not force). That’s why it should always be the last parameter (followed by the archive name).

Leave a Reply