How to use the tar command

To extract files from an archive:

tar -xf archive.tar.bz2

To list the contents of an archive:

tar -tf archive.tar.gz

To create a new archive:

tar -caf archive.tar.xz file(s)…

Wait, it’s that simple?

Yep.

So what’s the matter with things like -xzvf?

The option -z is nowadays superfluous when extracting or listing the contents of an archive. Most versions of tar in common use today (like GNU tar) can detect a compression method automatically. Options like -z, -j or -J are useful only when you’re creating an archive, and even then, they can be all replaced with option -a to choose the compression method automatically based on the file suffix of an archive (like .gz) given on the command line.

The option -v is used to output the file list of an archive when creating or extracting it. It is sometimes useful when you want to see the progress of creation or extraction of a bigger archive, otherwise there is no need to bother with it.

Other useful things to know

If you want to extract files from an archive to a directory different than the current one, you can use:

tar -xf archive.tar.gz -C directory

If you want to list not only names of files in the archive, but also their sizes, modification dates, permissions, etc., just add the option -v (yes, it’s another case when it’s useful):

tar -tvf archive.tar.bz2

If you want to achieve better compression, create the archive in two steps:

tar -cf archive.tar file(s)…
gzip -9 archive.tar

If you use a GNU operating system (like GNU/Linux) and want to see detailed help, open the info manual:

info tar

…or just read it online, since it’s usually more convenient.

‹ back to Pauline’s Meadow