Say I have a file.txt containing one line:



tar -czf archive.tar.gz file.txt

I can create a "gzipped archive" with the name archive.tar.gz, which is to my knowledge the equivalent of doing

tar -cf archive.tar file.txt
gzip archive.tar

Now, to extract archive.tar.gz, I would use

tar -xzf archive.tar.gz

to get my file.txt. This should be the equivalent of doing

gzip -d archive.tar.gz
tar -xf archive.tar

However, if I just use the command

tar -xf archive.tar.gz

without decompressing the file first through gzip or the --gzip option in tar, I still get my file.txt and can read the content.

I changed the file ending from archive.tar.gz to just archive.tar and got the same results.

How does tar know that it has to decompress the file first given that the file ending can be changed by the user? Do I miss some essential knowledge about file storing that allows tar to notice the compression?

1 Answer 1


tl;dr - GNU tar is smart

In the good old, bad old days you had to do exactly as you say and use -xzf. Nowadays, tar opens the archive, has a quick peek and if the contents look compressed then it invokes uncompress automatically for you. And that's it. You can take a look at man tar to see what compression algorithms it supports.

  • 1
    Oh, okay. I guess I should stop using ss64.com as reference for unix commands and just use man. I took a look at tar and found what I was looking for: [...] this implementation recognizes gzip compression automatically when reading archives.
    – tim-kt
    Jan 13, 2019 at 6:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .