gunzip Command in Linux



gunzip is a command-line tool used in Linux to decompress files that have been compressed using the gzip utility. It restores the original files from its compressed state, typically removing the .gz extension.

The gunzip command is pretty useful in case you want to manage compressed files, thus making it easier to handle large datasets or archives. By decompressing files, gunzip helps save storage space and reduces file transfer times.

Table of Contents

Here is a comprehensive guide to the options available with the gunzip command −

Syntax of gunzip Command

Here's the basic syntax to use the gunzip command in Linux system −

gunzip [options] [archive_name]

Where,

  • gunzip keyword invokes the main command.
  • [options] are flags used optionally to modify the behavior of the command.
  • [archive_name] is the name of the file you want to decompress.

gunzip Command Options

The following are few options that can be used with the gunzip command in Linux −

Option Description
-c, --stdout Directs output to standard output while preserving the original files.
-f, --force Enforce decompression regardless of whether the file has multiple links or already exists.
-k, --keep Keep the original file after decompression.
-l, --list Decompress files in the current directory and its subdirectories.
-r, --recursive Decompress files in the current directory and its subdirectories.
-S, --suffix=SUF Use suffix SUF instead of .gz.
-t, --test Test the compressed file integrity.
-v, --verbose Verbose mode: show the file name and the percentage reduction for each file.

Examples of gunzip Command in Linux

The following are a few basic examples of gunzip command in Linux system −

  • Basic Decompression
  • Keep Original File
  • Decompress Multiple Files
  • Decompress to Standard Output
  • Test Integrity of Compressed File
  • Decompress Files Recursively

Basic Decompression

As previously mentioned, the gunzip command is used in Linux to decompress a file that was compressed previously using gzip. For example −

gunzip file.gz

The above command will decompress file.gz to file. This is the simplest use of this command, where it takes a .gz file and restores it to its original form.

Basic Decompression of gunzip command

Keep Original File

In case you are interested in keepin the original compressed file after decompression, you can simply use the -k option. For example −

gunzip -k file.gz

Running the above command will decompress file.gz to file and keep the original file.gz. This is useful in case you want to retain the compressed version for backup or other purposes.

Keep Original File of gunzip command

Decompress Multiple Files

You can also decompress multiple files at once using the gunzip by listing them all in the command. For example −

gunzip file1.gz file2.gz file3.gz

The above command will decompress file1.gz, file2.gz, and file3.gz. This is handy when you have several compressed files that you need to decompress in one go, saving you time and effort.

Decompress to Standard Output

If you want to decompress a file and write the output to another file, you can use the -c option followed by redirection. For example −

gunzip -c file.gz > outputfile

This command will decompress file.gz and write the output to outputfile. This is useful when you want to redirect the decompressed content to a different file or process it further without creating an intermediate file.

Decompress to Standard Output of gunzip

Test Integrity of Compressed File

To test the compressed file integrity without decompressing it, you can use the -t option. For example −

gunzip -t file1.gz

This command will test the integrity of file1.gz. It checks if the file is valid and not corrupted, which is useful for verifying backups or downloads before decompression.

Test Integrity of Compressed File of gunzip

Decompress Files Recursively

To decompress all .gz files in a directory and its subdirectories in Linux, simply use the -r option with the command. For example −

gunzip -r /path/to/directory

This command will recursively decompress all .gz files in the specified directory. This is particularly useful for decompressing large sets of files organized in a directory structure.

Force Decompression

To force decompression even if the file has multiple links or an existing corresponding file, you can use the -f option. For example −

gunzip -f file.gz

The above command will force the decompression of file.gz. It ensures that the decompression proceeds even if there are potential conflicts, such as existing files with the same name.

Force Decompression of gunzip

List Contents of a Compressed File

To list the contents of a compressed file, you can use the -l option. For example −

gunzip -l file.gz

This command will list the contents of file.gz. It provides information about the compressed file without actually decompressing it, which can be useful for quick inspections.

List Contents of a Compressed File of gunzip

Conclusion

The gunzip command is a Linux utility for decompressing files that have been compressed with the gzip utility. This guide has covered the syntax, various options, and practical examples of using the gunzip command.

By understanding and mastering the gunzip command, you can efficiently manage compressed files, and make it easier to handle large datasets or archives. Once you become proficient with this command, you can significantly improve your productivity in managing file storage and transfers on a Linux system.

Advertisements