andreas.schaertl

Compressing, Extracting and Listing with tar

tech

The Unix tool tar is a bit awkward to use because most of the time it isn’t called with flags like -l or -v. With tar, all flags are usually smashed together in the first argument.

Luckily, the flags for basic tasks, that is compressing, extracting and viewing, all follow the same pattern, which is easy enough to memorize:

    tar {c,x,t}vf{z,j} [archive file] [file]...

First comes tar, the program itself. Then you can choose from either c, x or t.

  • c is for compressing files into archives. [archive file] will be the name of the created archive and [file]... the files you want inside that archive.
  • x is for extracting files from [archive file]. In this case, [file] is omitted.
  • t is for listing files from [archive file] (I guess the word list does have the letter t in it). Again, [file] is omitted.

Then comes v for verbose output (lets you know what is happening) and f for file because per default tar likes to read and write from stdin. Finally there are the optional z or j flags. They enable compression.

  • z uses gzip compression, which is fast but not as efficient as some other algorithms. Your archive file should have the .tar.gz extension.
  • j uses bzip2, which is slower but more efficient than gzip. A bzip2 archive should have a .tar.bz2 extension.

When extracting files (x flag), you can actually tell tar to pick the correct compression algorithm on its own. Instead of z or j, use a like this:

    tar xvfa [archive file]

Now tar is way more powerful than this, but for daily use, I find this to be enough.