Linux gunzip Command: 10 Examples with Details

The gunzip command in Linux is used to decompress files that were compressed using gzip, typically identified by the .gz extension. Below are 10 practical examples demonstrating various uses of the gunzip command, complete with explanations and scenarios.

Example 1: Decompress a Single File

Decompress a single .gz file.

gunzip document.txt.gz

Explanation: This command decompresses document.txt.gz, replacing it with the uncompressed file document.txt in the same directory.

Use Case: Uncompressing a single text file received via email or download for editing.

Example 2: Decompress Multiple Files

Decompress multiple .gz files in the current directory.

gunzip *.gz

Explanation: The *.gz wildcard matches all files with the .gz extension in the current directory and decompresses them, replacing each with its uncompressed version.

Use Case: Uncompressing a batch of log files for analysis.

Example 3: Keep the Original Compressed File

Decompress a file while retaining the original .gz file.

gunzip -k document.txt.gz

Explanation: The -k (keep) flag decompresses document.txt.gz to document.txt without deleting the original compressed file.

Use Case: Creating an uncompressed copy for immediate use while keeping the compressed file for archiving.

Example 4: Force Overwrite During Decompression

Decompress a file and overwrite an existing file without prompting.

gunzip -f document.txt.gz

Explanation: The -f (force) flag decompresses document.txt.gz and overwrites document.txt if it already exists, without asking for confirmation.

Use Case: Automating decompression in scripts where overwriting is acceptable.

Example 5: Verbose Output During Decompression

Display detailed output while decompressing.

gunzip -v document.txt.gz

Explanation: The -v (verbose) flag shows the name of the file being decompressed and the compression ratio, useful for monitoring the process.

Use Case: Confirming that decompression is occurring correctly during batch operations.

Example 6: Decompress to a Specific Directory

Decompress a file and redirect the output to a specific directory.

zcat document.txt.gz > /home/user/output/document.txt

Explanation: While gunzip itself doesn’t support redirecting output to a different directory, zcat (a related command) decompresses document.txt.gz and pipes the output to /home/user/output/document.txt, leaving the original .gz file intact.

Use Case: Extracting files to a specific location without modifying the original compressed file.

Example 7: Test Compressed File Integrity

Check the integrity of a compressed file without decompressing it.

gunzip -t document.txt.gz

Explanation: The -t (test) flag verifies the integrity of document.txt.gz without extracting it, reporting any corruption issues.

Use Case: Ensuring a downloaded compressed file is intact before decompression.

Example 8: Decompress and Display Content

View the contents of a compressed file without saving the decompressed file.

gunzip -c document.txt.gz

Explanation: The -c flag decompresses document.txt.gz and outputs the content to the terminal (stdout) without creating a new file or deleting the original.

Use Case: Quickly inspecting the contents of a compressed file without extracting it.

Example 9: Recursive Decompression in a Directory

Decompress all .gz files in a directory and its subdirectories.

find /home/user/data -name "*.gz" -exec gunzip {} \;

Explanation: The find command locates all .gz files in /home/user/data and its subdirectories, and -exec gunzip {} \; runs gunzip on each file, decompressing them in place.

Use Case: Uncompressing a large number of files scattered across multiple subdirectories.

Example 10: Decompress and Rename Output

Decompress a file and save it with a different name.

gunzip -c document.txt.gz > new_document.txt

Explanation: The -c flag decompresses document.txt.gz and pipes the output to new_document.txt, preserving the original compressed file.

Use Case: Creating a renamed decompressed file for use in a different context while keeping the original.

Additional Notes