What is the difference with all the different types of .tar archives?

Solution 1:

The .gz, .xz, .bz2, .7z, .lz, .z suffixes are used to make clear that the file is compressed with said compression algorithm. Its use is not limited to .tar files, you will also see files such as initrd.gz (an compressed initial ramdisk) or manual.txt.gz (for a compressed text document). You may also see file.tgz which is also indicates a gzip-compressed tarball (like file.tar.gz).

The file extension is a convention, it does not necessarily describe the file contents. To check what kind of file you are handling, use the file command. Example:

$ file data.tar.gz
data.tar.gz: gzip compressed data, from Unix
$ gunzip -c data.tar.gz | file -
/dev/stdin: POSIX tar archive (GNU)

In my experience, gzip-compressed files (.gz) are the most common ones. It is fast in compressing and decompressing, though there are other algorithms that yield better compression ratios.

Solution 2:

In addition to the @Lekensteyn answer, I'd like to add that this behavior is based on Do one thing and do it well philosophy - tar program knows how to put multiple files into a single stream (with .tar extension), gzip knows how to compress a single file (adding .gz extension)*. By combining those tools you can create a command which compresses multiple files into a single .tag.gz file.

This approach is very flexible - you can combine a few standard Unix commands to do all sorts of things. For example, if you write a better program to compress files you don't need to modify tar to enable it to create .tar.mymegazip files - instead, you just pipe tar's output to your compressor.

Similarly, tar knows nothing about SSH, but by combining commands you can create a script which archives files, uploads them to a remote machine via SSH and un-archives them there.

Compare this to zip command, which did not originate in Unix - it has built-in tools to compress whole directories, encrypt files, split the archive into smaller zipfiles etc.


(footnote) - yes, tar is able to gzip or bzip files itself using -z and -j switches, they were added for convenience, and GNU tar is actually spawning gzip or bzip2 as a new process.