Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I run

tar -cvzf archive.tgz file1 file2 ; rm file1 file2

it normally creates a compressed tarball, but how is it possible that

tar -xvf archive.tgz

gives me back the uncompressed files? I've always thought that the -z flag would be required.

share|improve this question
    
Apparently, the creators of UNIX also think that that's how tar should behave: 9p.io/wiki/plan9/UNIX_to_Plan_9_command_translation/index.html – PSkocik Jan 6 at 14:08
1  
@PSkocik Sure, it breaks the unix philosophy: Write programs that do one thing and do it well. Write programs to work together. – jlliagre Jan 6 at 17:55
    
@jlliagre I don't think so. Being able to infer that you need to gunzip when the file is gzipped doesn't imply your program must be monolithic. – PSkocik Jan 6 at 18:08
    
@PSkocik You are right. At least GNU, Solaris and busybox tar implementations all use the external gzip utility so do not duplicate code. – jlliagre Jan 6 at 21:57
up vote 16 down vote accepted

Your tar implementation, likely the GNU one, is detecting the file passed as a parameter is compressed.

The mostly used tar implementations these days, GNU tar and busybox ones, are looking to the first bytes of the file, a.k.a. magic number, to figure out if it is compressed and the compression algorithm to use.

The tar implementations found on commercial Unixes that are based on the original AT&T code do not support the -z flag in the first place. One notable exception is Solaris 11 tar where this extension has been added, including the ability to detect the file format.

share|improve this answer
    
I thought it looks at the suffix. It also has to know which compression method has been used to incompress. Would it do that correctly if the tar file was suffixed incorrectly? – Faheem Mitha Jan 6 at 12:02
1  
@FaheemMitha, not rocket science, just look for file(1). – vonbrand Jan 6 at 12:21
    
Actually, multiple tar implementations auto-detect whether the content is compressed. A good answer would point this out, and how it is done. – Thomas Dickey Jan 6 at 12:33
    
@ThomasDickey go for it. – Faheem Mitha Jan 6 at 13:20
    
@ThomasDickey I didn't wrote other tar implementation never do, just that GNU tar being by far the most commonly used one these days, it was likely the one used by the OP. Note too that the original tar command (AT&T) didn't support -z in the first place. – jlliagre Jan 6 at 13:57

Star introduced automated compression detection and automated decompression aprox. 20 years ago.

These days, some other tar implementations, like gtar copied the idea.

BTW: star understands the magic numbers in the compressed stream to detect the correct decompression method. Also note that gtar was reported to fail when a compressed archive is read from stdin instead from a named file.

share|improve this answer

The -z option is not required in two well-known implementations of tar: GNU tar and BSD tar (aka libarchive). Detecting the compression algorithm needed is trivial, done by inspecting the first few bytes of the input file.

Whether you happen to be using one or the other of those implementations largely depends upon which operating system you are using: Linux-based systems or BSD systems (including OSX).

I have summarized details of when these features were introduced on my page about tar versus portability.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.