gzip (stands for General file (de)compression) is one of the most popular compression algorithms that allows you to reduce the size of a file and free up some space when the file system is running out of disk space.
It compresses the file with a
'.gz' extension and preserves file permissions, ownership patterns (RWX), access and modification timestamps. It compresses regular files and ignores symbolic links.
It is most popular in Linux and Unix operating systems and is still widely used today.
Make a Note: gzip compresses only single files and creates a compressed file for each given file, not for a directory. If you want to compress multiple files or directory into one file, you should use tar archive.
|-k –keep||Keep (don’t delete) input files during compression or decompression.|
|-c –stdout||Write output on standard output and keep original files unchanged.|
|-l –list||To displays the statistics of the compressed files.|
|-r –recursive||Travel the directory structure recursively and compress all the files.|
|-d –decompress||To decompress a file|
|-1 –fast||To get minimum compression ratio at the fastest speed|
|-9 –best||To get maximum compression ratio at the slowest speed|
Installing gzip in Linux
By default the gzip package would have already installed. However, if it is not installed, you can install it now using the package manager as shown below
yum install gzip #RHEL 7 dnf install gzip #RHEL 8/9, Fedora apt install gzip #Debian/Ubuntu zypper install gzip #OpenSUSE pacman -S gzip #Arch Linux
The general syntax for the gzip command is as follows:
gzip [Option] [File_Name]
Compressing files with gzip
To compress a file, use the filename followed by the ‘gzip’ command.
In this example, gzip creates the file ‘myfile.gz’ and deletes the original file.
Keep the original file with gzip
By default, gzip removes the original file, but if you want to keep it, use the
gzip -k myfile
Also, you can use the
'-c' option with gzip to keep the original files unchanged. It writes output on standard output and redirects output to a file.
gzip -c myfile > myfile.gz
List the compressed file contents
gzip displays the statistics of the given compressed files with the
'-l' option, but not the actual file contents.
gzip -l myfile.gz compressed uncompressed ratio uncompressed_name 41 13 0.0% myfile
It shows, following details:
- compressed size: size of the compressed file
- uncompressed size: size of the uncompressed file
- ratio: compression ratio (0.0% if unknown)
- uncompressed_name: name of the uncompressed file
'-v' option to get more information:
gzip -lv myfile.gz method crc date time compressed uncompressed ratio uncompressed_name defla 0762e828 Feb 24 00:55 41 13 0.0% myfile
Use the ‘zcat’ command to view the actual contents of a gzip compressed file.
zcat myfile.gz This is a test file for 'gzip' article preparation
Compressing multiple files
You can also compress multiple files at once, but it will create separate compressed files for each given file.
For example: To compress the files named myfile1, myfile2, myfile3, you would run the following command and it will create three compressed files named myfile1.gz, myfile2.gz, myfile3.gz.
gzip myfile1 myfile2 myfile3
Compressing all files in a directory
To compress all files in a given directory, use the
It recursively go across the entire directory structure and compresses all files in the directory and its sub-directories.
gzip -r mydirectory
Understanding compression level
gzip allows to change the compression levels, from 1 to 9. The speed and level of compression vary based on levels. By default, it use compression level
-1 or --fastindicates the fastest compression method (less compression ratio).
-9 or --bestindicates the slowest compression method (best compression ratio).
To get maximum compression, run:
gzip -9 myfile
Decompressing files with gzip
To decompress a file using the gzip command, use the
gzip -d myfile.gz
Alternatively, you can use the ‘gunzip’ command to decompress a file. This is an alias of ‘gzip -d’.
In this tutorial, we’ve shown you how to use gzip command to Compress and Decompress files in Linux.
If you have any questions or feedback, feel free to comment below.