The Tar basics

Tar is a compression utility used to generate a single archive, known as a “tarball”, out of multiple files and subdirectories. It can also be used, inversely, to extract these same subdirectories and files out of the archive.

I want to create a tarball:

tar cvf [archive-name].tar [source-directory]

Here’s what the options mean:

c = Create a new archive
v = Verbosely (i.e. tell me what you’re doing as you do it)
f = named as Follows

If you want to compress the tarball, add the Z option. Remember to add the .gz suffix to the name:

tar czvf [archive-name].tar.gz [source directory]

You can also do this after, as a separate step, using gzip.

I want to extract the contents of a tarball:

The options are the same except for the first:

x = eXtract the archive

tar xvf [archive-name].tar

If the tarball has been compressed, add the Z option to uncompress it first, then extract the archive:

tar xzvf [archive-name].tar.gz

I want to extract the contents of a tarball into another directory:

tar xvf [archive-name].tar -C [/path/to/target/directory]

Note: Tar won’t create the target directory, so if it doesn’t already exist be sure to mkdir first.

Add the --strip-components=1 flag if your tarball consists of files and subdirectories inside a parent directory and you only want to extract the files and subdirectories out of the parent.

I need a ton of other very particular options like extracting a single file or viewing a list of the archive without extracting it:

Geek Stuff has a very comprehensive of options I’ve never had reason to use before.