Uncompressing Files

Problem

You need to uncompress one or more files ending in extensions like tar, tar.gz, gz, tgz, Z, or zip.

Solution

Figure out what you are dealing with and use the right tool. Table 8-3 maps common extensions to programs capable of handling them.

Table 8-3. Common file extensions and compression utilities

File extension

Command

.tar

tar tf (list contents), tar xf (extract)

.tar.gz, .tgz

GNU tar: tar tzf (list contents), tar xzf (extract)

else: gunzip file && tar xf file

.tar.bz2

GNU tar: tar tjf (list contents), tar xjf (extract)

else: gunzip2 file && tar xf file

.tar.Z

GNU tar: tar tZf (list contents), tar xZf (extract)

else: uncompress file && tar xf file

.zip

unzip (often not installed by default)

You should also try the file command:

$ file what_is_this.*
what_is_this.1: GNU tar archive
what_is_this.2: gzip compressed data, from Unix

$ gunzip what_is_this.2
gunzip: what_is_this.2: unknown suffix -- ignored

$ mv what_is_this.2 what_is_this.2.gz

$ gunzip what_is_this.2.gz

$ file what_is_this.2
what_is_this.2: GNU tar archive

Discussion

If the file extension matches none of those listed in Table 8-3 and the file command doesn’t help, but you are sure it’s an archive of some kind, then you should do a web search for it.

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.