Have you come across files bearing a .gz extension? These are files which have been compressed using the gzip command. Gunzip is a Linux command that is used to decompress such files earing the .gz extension and in this tutorial, we will look at different usages of the Gunzip command.
But first, let's compress a file using the gzip command. The syntax will be as follows
# gzip file_name
When you verify the file using the ls command, you will notice the presence of another file with the same file name but with a .gz extension.
Let's now see how we can decompress files.
1. Decompress files using the Gzip Command
In addition to compressing files, the gzip command can also be used to decompress files. The syntax for decompressing a file is
# gzip -d file_name.gz
To decompress files recursively in a folder add the -r flag as shown
# gzip -dr folder_name
2. Decompress files using the Gunzip Command
While the gzip command also comes in handy in compressing and decompressing files, Gunzip command is another command that allows you to decompress files in a very simple way that's easy to remember. The syntax is
# gunzip file_name.gz
Once gunzip decompresses a file, the extension is removed. The file file_name.gz changes to file_name and is expanded to the maximum size.
3. Display verbose output of decompression
To display verbose output, append the -v flag as shown
# gunzip -v file_name.gz
test_file.gz: 52.1% -- replaced with test_file
4. Keep both the compressed file and the decompressed one
To keep both copies of the compressed and decompressed file run
# gunzip -k file_name.gz
You will now have two files, filename & filename.gz
5. Display the output of the compressed file without first decompressing
To print out the output of the compressed file before decompressing it run
# gunzip -c file_name.gz
6. To display more information about a compressed file
To get more information about the compressed file run
# gunzip -l file_name.gz
The output of the command above command will give the following values
- Compressed size
- Uncompressed size
- Ratio of compression
- Uncompressed name
This information comes in handy when dealing with large file sizes, especially when you are running low on disk space. You wouldn't want to carelessly uncompress large files lest they eat up your remaining disk space.
7. To decompress lot's of files recursively
To achieve this, run
# gunzip -r folder_name
Let's assume you have a folder structure like below where Office_files is the main folder containing zipped files sales.gz and marketing.gz plus another folder 2018 report with other zipped files.
8. To decompress all the files within a directory
Run below command to decompress all of the files in the directory.
# gunzip -r office_files
9. Test whether a compressed file is a valid file compressed using gzip
To do this, run
# gunzip -t file_name.gz
If the file is invalid, you'll get a warning but if it's valid, nothing will be printed on the screen and you'll be taken back to the shell.