How to use the command gunzip (with examples)

How to use the command gunzip (with examples)

The gunzip command is used to extract files from a gzip (.gz) archive. It decompresses the files and replaces the compressed files with the extracted ones. This command is useful for retrieving individual files from compressed archives or working with compressed files in general.

Use case 1: Extract a file from an archive, replacing the original file if it exists

Code:

gunzip archive.tar.gz

Motivation: If you have a compressed archive archive.tar.gz and you only need to extract a specific file from it, you can use this gunzip command. It will decompress the archive, replace the original compressed file with the extracted one, and keep the extracted file in the same location with the same name.

Explanation:

  • gunzip is the command to decompress the archive.
  • archive.tar.gz is the name of the compressed file you want to extract.

Example Output: The file archive.tar.gz will be extracted, and the resulting file archive.tar will replace the original compressed file if it exists.

Use case 2: Extract a file to a target destination

Code:

gunzip --stdout archive.tar.gz > archive.tar

Motivation: If you want to extract a file from a compressed archive but want to specify the destination where the extracted file should be saved, you can use this gunzip command. By using --stdout and redirecting the output to a file using >, you can specify the location and name of the extracted file.

Explanation:

  • gunzip is the command to decompress the archive.
  • --stdout is an option that directs the decompressed content to the standard output.
  • archive.tar.gz is the name of the compressed file you want to extract.
  • > is the redirection operator that saves the output to a file.
  • archive.tar is the target destination where the extracted file will be saved.

Example Output: The file archive.tar.gz will be extracted, and the resulting file will be saved as archive.tar in the specified target destination.

Use case 3: Extract a file and keep the archive file

Code:

gunzip --keep archive.tar.gz

Motivation: If you need to extract a file from a compressed archive but also want to keep the original compressed file, you can use this gunzip command. It decompresses the archive, replaces the compressed file with the extracted one, and keeps the original file as well.

Explanation:

  • gunzip is the command to decompress the archive.
  • --keep is an option that retains the original compressed file after extraction.
  • archive.tar.gz is the name of the compressed file you want to extract.

Example Output: The file archive.tar.gz will be decompressed, and the resulting file will replace the original compressed file. The original compressed file, archive.tar.gz, will still exist.

Use case 4: List the contents of a compressed file

Code:

gunzip --list file.txt.gz

Motivation: If you want to see the contents of a compressed file without extracting it, you can use this gunzip command. It will display the list of files contained within the compressed file.

Explanation:

  • gunzip is the command to decompress the archive.
  • --list is an option that lists the contents of the compressed file without extracting it.
  • file.txt.gz is the name of the compressed file you want to list.

Example Output: The command will display the names of the files contained within the compressed file file.txt.gz.

Use case 5: Decompress an archive from stdin

Code:

cat path/to/archive.gz | gunzip

Motivation: If you have a compressed archive and want to decompress it directly from stdin, you can use this gunzip command. It allows you to pipe the content of the compressed file from stdin using cat and decompresses it.

Explanation:

  • cat is a command used to read the contents of files and concatenate them.
  • path/to/archive.gz is the path to the compressed archive file.
  • | is the pipe operator, which passes the output of cat as the input to gunzip.
  • gunzip is the command to decompress the archive.

Example Output: The compressed file will be read using cat, piped to gunzip, and decompressed, resulting in the extraction of the archive’s contents.

Conclusion:

The gunzip command is a versatile tool for working with compressed files and archives. It provides various options to extract files from compressed archives, specify destinations, list contents, and decompress directly from stdin. Understanding how to use these different options allows you to efficiently work with compressed files in your Linux environment.

Related Posts

How to use the command eksctl (with examples)

How to use the command eksctl (with examples)

eksctl is the official CLI for Amazon Elastic Kubernetes Service (EKS).

Read More
How to use the command mutagen (with examples)

How to use the command mutagen (with examples)

Mutagen is a real-time file synchronization and network forwarding tool. It allows users to synchronize files and directories between local and remote systems, as well as between local systems and Docker containers.

Read More
Using the Netlify Command (with examples)

Using the Netlify Command (with examples)

Netlify is a popular platform for hosting and deploying websites. The netlify command is a powerful tool that allows developers to manage their Netlify sites from the command line.

Read More