use gunzip command in Linux 1

How to Use gunzip Command in Linux

Gunzip, a command-line utility, is used in Linux to decompress compressed files. The compressed files can include images, audio, documents, etc. Furthermore, you can use the gunzip command in Linux to decompress several file extensions including .tgz and .taz files.

Gunzip utility can decompress files that have been compressed using zip, pack or gzip application. It can detect the format of the file automatically. Furthermore, this command can extract file extensions such as .Z, -z, .z, .gz, -gz, .tgz or .taz. In addition, after you have completed the decompression process, the file will be available in its original format. 

In this article, we will look at several ways you can use the gunzip command to decompress the zipped files. 

Synopsis

The gunzip command has a simple syntax. However, you can also use flags with this command to alter its behaviors. The syntax of the gunzip command is shown below:

gunzip [Option] [archive name/file name]

Additionally, the possible combination of flags that you can use with the gunzip commands are shown in this syntax:

gunzip [ -acfhlLnNrtvV ] [-S suffix] [ name ... ]

You can also take a look at the description of each flag as listed below:

  • -a, –ascii: ASCII text mode
  • -c: Write the output on the console and keep the original files unchanged. 
  • -d, –decompress: Un compresses the given file.
  • -f, –force: Forces the compression even if it already exists. However, it prompts if the user wants to replace the existing file.
  • -h, –help: Displays the help menu. 
  • -L, –license: Displays the license of the gunzip command. 
  • -n, –no-name: Does not save the original file or timestamp when decompressing the file. 
  • -N, –name: Saves the original file and timestamp when compressing the file. Also, it restores the original filename and timestamp when decompressing the file. 
  • -q, –quiet: Stops all the warnings from appearing on the screen. 
  • -r, –recursive: Searches the directory structure continuously. 
  • -t, –test: Tests the integrity of the compressed file. 
  • -v, –verbose: Displays the name of the compressed/decompressed file. 
  • -V, –version: Displays the version number. 
  • -#, –fast, –best: Increases or decreases the speed of the compression. The digit -1 or –fast represent the fastest compression method. In contrast, -9 is the slowest compression. The average compression level is -6. 

1. Use the gunzip Command to Decompress the Files

To use the gunzip command to decompress any file, type the command followed by the filename. This is the most basic method to use this command. 

Syntax:

gunzip filename.gz

For Example:

gunzip index.html.gz
gunzip index

Additionally, you can also use the -d flag with the gunzip command to perform the same operation. Furthermore, you can verify that the file has been compressed using the ls command. You will see that the original file disappears after the decompression. 

2. Use the –k Flag with the gunzip Command to Keep the Original Files

After using the gunzip command to decompress, the original compressed file is no longer available. However, if you want to retain the copy, you will use the -k flag with the gunzip command. 

Syntax:

gunzip -k filename.gz

For example:

gunzip -k myFolder.gz

3. Use the -c Flag to Display the Contents

To display the file contents while decompressing, you will use the –c flag with the command.

Syntax:

gunzip -c filename.gz

For example:

gunzip -c index.html.gz

You can see the contents of the file as shown in the screenshot below:

gunzip with -c flag

4. Use the -C flag to Compress to Another Folder

You can also use the gunzip command to decompress the files into another folder. For this step, you will use the –c flag as shown below:

gunzip -c filename.gz > /anotherdirectory/path/

For example:

gunzip -c myFolder > /myNewFolder

5. Decompress Multiple Files using the gunzip Command

With the help of the gunzip command, you can also decompress multiple files at the same time.

For example:

gunzip file1.gz file2.gz file3.gz

6. Use the -r Flag to Decompress Continuously

To decompress all files recursively, we will use the -r flag as shown below:

gunzip -r directoryName

For example:

gunzip -r myFolder

7. Use the -l Flag to View the Output Information

If you want to view the output information of the operation performed through the gunzip command, use the –l flag with the command. This will enable you to view the size and timestamp.

For example:

gunzip -l index.html.gz

From the above image, you can see that the output of this command includes the size of both the compressed and the uncompressed files.

gunzip command with  -l flag

8. Use the -v Flag to Get User-Friendly Output

To get the verbose or user-friendly output of the compression/decompression process, use the –v flag.

Syntax:

gunzip -v filename.gz

For example:

gunzip -v index.html.gz
Use the gunzip command to check verbose information

9. Use the -f Flag to Implement Force Decompression

Often it happens that the file you are trying to extract does not decompress. This is, in fact, due to external factors. Those external factors could be, for example, the existence of the file with the same name. You will use the –f flag with the gunzip command to handle this issue to force the decompression.

Syntax:

gunzip –f filename.gz

For example:

gunzip –f myFolder

However, in this process, the other existing file gets lost.

10. Use the –t Flag to Check the Integrity of the File

Before decompressing any file, you can also check its integrity. For this step, you can use the gunzip command with the –t flag.

Syntax:

gunzip –t filename.gz

For example:

gunzip -t myFile.gz

If you do not receive any message or any output in return, it means that the file is valid and has been compressed properly. However, it will display the message if the file is invalid.

11. Use the -V flag to Check the gunzip Version

To check the version of the gunzip command-line utility, you can use the –V flag as shown below:

gunzip -V
Use the gunzip command to check version in Linux

Finally, you have mastered how to use the gunzip command in Linux. You have also learned how to use various flags for different scenarios. To get an in-depth view, you can check out more details in its official documentation manual.

If this guide helped you, please share it.

Leave a Reply
Related Posts