Apr 14, 2023

How to Unzip GZ and TAR Files on Linux

How to Unzip GZ and TAR Files on Linux

On Linux and Unix-like operating systems such as Ubuntu and MacOS, GZ and TAR files are two different types of archive files used to compress and package one or more files into a single file.

A GZ file (i.e. .gz) is a compressed version of a single file created with the gzip command.

A TAR file (i.e. .tar.tar.gz) on the other hand, is a type of archive file used to group multiple files together without compression created with the tar command.

Unzip GZ files with gzip

To decompress a GZ file you can use the gzip command with the -d flag (short for delete):

$ gzip -d file.gz

Which will delete the GZ file, and replace it with its decompressed version, without the .gz file extension.

For example:

$ gzip -d file.gz

$ ls
file

If you want to also keep the original GZ file, you can combine the previous command with the -k flag (short for keep):

$ gzip -dk file.gz

For example:

$ gzip -dk file.gz

$ ls
file   file.gz

Alternatively, you can use the gunzip command, which is basically an alias of the gzip -d command.

$ gunzip -k file.gz

Extract a TAR archive with tar

To unpack a TAR archive in the current directory, you can use the tar command with the -x and -f flags as follows:

$ tar -xf archive.tar

Where:

  • The -x flag is used to indicate that we want to extract the files contained in the archive.
  • The -f flag is used to specify the path of the archive we want to unpack.

If you want to an archive contents in a different directory than the current working directory, you can use the -C flag followed by the path of the target directory:

$ tar -xf archive.tar -C ~/Documents

Extract specific files

To list the files and directories contained in a TAR archive without unpacking it, you can use the -t and -f flags:

$ tar -tf archive.tar

Where:

  • The -t flag is used to list the content of the archive into the standard output (i.e. stdout).
  • The -f flag is used to specify the path of the archive we want to list the content of.

For example:

$ tar -tf pictures.tar
holidays/
holidays/japan/
holidays/japan/tokyo.jpeg
birthdays/
birthdays/2023/
birthdays/2023/cake.png

Once you know which files and directories you want to extract, you can list them right after the unpack command as follows:

$ tar -xf archive.tar file dir

Filtering files with patterns

Another method for specifying the files you want to extract from an archive is to use the --wildcards flag:

$ tar -xf archive.tar --wildcards <glob>

Where:

  • glob represents a globbing pattern (e.g. *.jpeg, ca?.txt).

For example:

$ tar -tf server.tar
server/README.md
server/index.js
server/package.json
server/server.js

$ tar -xf server.tar --wildcards "*.js"

$ ls server
index.js  server.js

Note that for the versions of the tar command that don't include the --wildcards flag (e.g. MacOS), the globbing pattern can be specified right after the command as follows:

$ tar -xf server.tar "*.js"

Removing intermediate directories

When extracting a file contained in a directory and one or more subdirectories, each directory of the specified path will be created upon execution.

For example:

$ tax -xf pictures.tar holidays/japan/tokyo.jpeg

$ ls
holidays  pictures.tar

$ ls -R holidays
japan

holidays/japan:
tokyo.jpeg

To get rid of these intermediate directories and extract the file only, you can use the --strip-components flag:

$ tar -xf archive.tar --strip-components=number /path/to/file

Where:

  • number represents the number of leading path elements to be removed.

For example:

$ tar -xf pictures.tar --strip-components=2 holidays/japan/tokyo.jpeg

$ ls
pictures.tar  tokyo.jpeg

Related posts