Extract single file/directory from tar gz archive

Doing automated backups is a must for every sysadmin worth its’ salt. If you happen to administer a smaller pool of servers/hosts it is more than likely that you have your backups tarballed and stored somewhere relatively safe.

Restoring from such a backup is an easy process, but what if you only need to fetch a single file or directory from that .tar / .tar.gz file?

First, you should list out the contents of the archive (and preferably pipe it to grep to find what you need):
tar -tvf backup.tar

With grep passed on as well:
tar -tvf backup.tar | grep directoryname

After you’ve found what you are looking for, it’s time to extract it using:
tar -xvf backup.tar var/www/directoryname