Linux – Extracting a tar.gz file returns, “This does not look like a tar archive.”

archivingcompressionlinuxtarunix

I have a tar.gz file and I want to extract it using terminal. I used following commands: tar tvzf ldtp_3.5.0.orig.tar.gz and tar -xvzf ldtp_3.5.0.orig.tar.gz. However, I get the same result for both the files:

tar: This does not look like a tar archive
tar: Skipping to next header
tar: Exiting with failure status due to previous errors

I output of the file command shows its a gzip file:

$ file ldtp_3.5.0.orig.tar.gz 
ldtp_3.5.0.orig.tar.gz: gzip compressed data, from Unix

I can extract it using Right-click –> Extract but I want to do that using terminal. Can anyone tell me how to do that?

Best Answer

Looking at the error you are seeing coupled with the fact that you can indeed decompress it says that the file is a genuine TAR/Gzip archive, but perhaps not an immediate TAR is created after the Gzip.

Meaning, if you downloaded this from a web server, sometimes Gzip compression get’s applied to web content on the server level to speed up content download. But if not properly set on the server to ignore already compressed content such as this, it can inadvertently double-Gzip files.

With that in mind I would see if simply un-Gzip it from the command line to see what happens. Use a command like this:

gzip -d ldtp_3.5.0.orig.tar.gz

And see what the resulting file is. I have a funny feeling after you run that command, you will not see a ldtp_3.5.0.orig.tar in the directory but rather a file named like the source—ldtp_3.5.0.orig.tar.gz— but perhaps with modification changed? If that is the case it is truly double-Gzipped. So at this point—after that initial un-Gzip—you can run the tar command you have like this:

tar -xvzf ldtp_3.5.0.orig.tar.gz

And it should now properly decompress the full TAR archive.

Another way of doing something similar to that two step process is to pipe the output of gzip to tar like this:

gzip -dc ldtp_3.5.0.orig.tar.gz | tar -xvzf -

The c in the gzip as well as the - at the end of the command is the key:

  • The c option for gzip will tell gzip to decompress to the standard output stream.
  • The - at the end of the tar command means “use standard input.” So coupled with the the -c in gzip, the pipe will then take the standard output stream piped to it from gzip and connect that to standard input in tar.