How to Use GUnzip to Decompress Files in Linux The gunzip command is a core Linux command-line utility used specifically to decompress files packed with the Gzip (.gz) algorithm. By default, running gunzip expands a compressed file back into its original form and automatically deletes the original .gz archive.
This comprehensive guide covers the essential syntaxes, practical examples, and advanced flags for using gunzip effectively in Linux. 1. Basic Decompression
To unpack a single .gz file, pass the filename as the main argument: gunzip archive.txt.gz Use code with caution.
Result: Restores archive.txt in the current working directory.
Note: The source file archive.txt.gz is removed upon completion. 2. Keep the Original Archive File
If you want to prevent gunzip from deleting the source .gz file, use the -k (or –keep) flag: gunzip -k archive.txt.gz Use code with caution.
Result: You will have both archive.txt.gz and the extracted archive.txt file side by side. 3. Read Content Without Extracting
You can view the text contents of a compressed file directly inside your terminal window using the -c flag: gunzip -c archive.txt.gz Use code with caution.
Result: Prints the text data to standard output (stdout) while keeping the archive file unchanged.
Alternative: You can achieve this exact same result using the built-in Linux zcat utility. 4. Decompress to a Different Directory
Because gunzip does not have a native “destination directory” switch, you must combine the standard output stream (-c) with a redirection operator (>): gunzip -c archive.txt.gz > /path/to/destination/archive.txt Use code with caution.
Result: Extracts the content to the designated target path and leaves your source .gz file intact. 5. Batch and Recursive Decompression
The tool handles operations involving multiple files and directory structures efficiently. Decompress Multiple Files Explicitly
To decompress several individual files simultaneously, separate them by spaces: gunzip file1.txt.gz file2.txt.gz file3.txt.gz Use code with caution. Decompress Using Wildcards
To decompress every .gz archive file found inside your current folder, use the * wildcard pattern: gunzip.gz Use code with caution. Recursive Directory Unpacking
To look through an entire directory chain and decompress every subfolder’s .gz contents, combine the -r flag with a folder path: gunzip -r /home/user/my_folder/ Use code with caution. 6. Inspect Archive Metadata
If you need to check the space savings or verification data without performing a full expansion, invoke the listing flag (-l): gunzip -l archive.txt.gz Use code with caution. How do you gunzip a file and keep the .gz file?
To gunzip a file and keep the .gz file, you can use the following commands: * gunzip -c compressed-file.gz > decompressed-file Super User How to decompress files in linux. Gunzip command.