How to extract .gz or .tar.gz files on Linux

Jul 03, 2024#linux#cli#bash

A .gz file is a compressed file format commonly used in Unix-based systems. The .gz extension indicates that the file has been compressed using the gzip (GNU zip) compression algorithm.

While gzip is typically associated with Unix-based systems (like Linux and macOS), tools to create and extract .gz files are also available for Windows like 7-Zip or WinZip.

The gzip algorithm compresses files by eliminating redundancy, making the file size smaller. This is particularly useful for saving disk space and reducing the time required to transfer files over networks.

Gzip is often used in combination with other tools. For example, a .tar.gz file is a tar archive (which can contain multiple files and directories) that has been compressed with gzip. This combination is commonly used for packaging software distributions and backups.

For single file

Gzip compresses a single file and adds a .gz extension to the compressed file.

To compress a single file, use the following command:

gzip filename

Replace filename with the name of the file you want to compress.

To decompress a .gz file, use the -d option:

gzip -d filename.gz

You can use the gunzip command:

gunzip filename.gz

For multiple files

Gzip is designed to compress single files, but it can be used in combination with other tools to compress multiple files.

To compress multiple files, you typically use the tar command in combination with gzip. The tar command can archive multiple files into a single tarball, and gzip can then compress that tarball. This results in a .tar.gz file (or .tgz for short).

tar -czvf archive.tar.gz file1 file2 directory
  • tar creates an archive.
  • -c creates a new archive.
  • -z compresses the archive with gzip.
  • -v provides verbose output (optional, shows progress).
  • -f specifies the filename of the archive.

To extract a .tar.gz file:

tar -xzvf archive.tar.gz
  • -x extracts the archive.
  • -z decompresses it with gzip.
  • -v provides verbose output (optional).
  • -f specifies the filename of the archive.