gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio and speed. The compressed files are typically given the .gz file extension.

1) To compress a file, the basic syntax is as follows:

gzip [options] [file]

For example, to compress a file named "file.txt", the command would be:

gzip file.txt

This command will create a new file named "file.txt.gz" in the same directory, which is the compressed version of the original file. The original file will still exist, but its size will be smaller than the compressed version.

2) To decompress a file, the basic syntax is as follows:

gunzip [options] [file]

For example, to decompress a file named "file.txt.gz", the command would be:

gunzip file.txt.gz

This command will create a new file named "file.txt" in the same directory, which is the decompressed version of the original file. The compressed file will still exist, but its size will be larger than the decompressed version.

3) To compress a file and remove the original file, the -c option can be used.

gzip -c file.txt > file.txt.gz

This command will create a new compressed file named "file.txt.gz" in the same directory and remove the original file "file.txt"

4) To decompress a file and remove the original file, the -d option can be used.

gunzip -d file.txt.gz

This command will create a new decompressed file named "file.txt" in the same directory and remove the original compressed file "file.txt.gz"

5) To compress multiple files at once, the -r option can be used.

gzip -r directory/

This command will compress all files in the directory "directory" and its subdirectories recursively.

6) To view the compressed files' information, the -l option can be used.

gzip -l file.txt.gz

This command will show the compressed file's information including compressed size, original size, and compression ratio.

7) To compress file with a different compression level, the -1 to -9 options can be used.

gzip -1 file.txt

This command will compress the file with the least compression ratio, where as the -9 option will compress the file with the most compression ratio.

8) To test a compressed file, the -t option can be used.

gzip -t file.txt.gz

This command will test the compressed file for errors and will return "file.txt.gz: OK" if the file is valid.

9) To compress a file in a different format, the --best or --rsyncable options can be used.

This command will compress the file with the best compression ratio, and the --rsyncable option will make the compressed file more suitable for use with the rsync command, which is a tool for synchronizing files and directories between different systems. The --rsyncable option ensures that small changes to the original file will result in small changes to the compressed file, which can greatly reduce the amount of data that needs to be transferred when synchronizing the file.

10) To compress a file and keep the original file's timestamp, the -n option can be used.

gzip -n file.txt

This command will compress the file and keep the original file's timestamp.

11) To decompress a file to a specific location, the -c option can be used.

gunzip -c file.txt.gz > /path/to/folder/file.txt

This command will decompress the file and save it to the specified location.

12) To decompress multiple files at once, the --decompress or --uncompress options can be used.

gunzip --decompress *.gz

This command will decompress all files with the .gz extension in the current directory.

13) To compress and decompress files using a different compression algorithm, the -S option can be used.

gzip -S .zz file.txt

This command will compress the file with a .zz file extension instead of the default .gz file extension.

In addition to the options and parameters already discussed, there are a few other things to keep in mind when using the gzip command:

  • By default, gzip will overwrite existing files without warning. To avoid accidentally overwriting important files, you can use the -k option to keep the original file, or the -c option to write the compressed data to standard output, which can then be redirected to a new file.
  • gzip can also be used to compress entire directories, including all of their subdirectories and files. To do this, use the -r option and specify the path to the directory you want to compress. This can be useful for compressing backups or other large sets of data.
  • gzip can also be used to compress multiple files at once. To do this, simply list the files you want to compress on the command line, like so:
gzip file1.txt file2.txt file3.txt

This will create new compressed files named "file1.txt.gz", "file2.txt.gz", and "file3.txt.gz" in the same directory as the original files.

  • gzip is not the only compression tool available for Linux. Other popular options include bzip2 and xz, which offer higher compression ratios but are slower than gzip.
  • Some other popular tools like tar also have built-in support for gzip, so you can compress and archive a directory at the same time:
tar -zcvf archive.tar.gz directory/
  • It is also important to note that gzip is not suitable for compressing certain types of files, such as files that are already compressed, or files that are very small. In such cases, the compressed file may actually be larger than the original file.

It is also worth noting that gzip is a lossless compression algorithm, which means that the original file can be exactly reconstructed from the compressed file. This is suitable for most types of files, but for certain types of files such as images or audio, lossy compression algorithms may be more appropriate.

Another important thing to note is that gzip does not provide any security features, such as encryption. If you need to compress sensitive files and keep them secure, you should use a tool that provides encryption, such as gpg or zip with password protection.

In terms of performance, gzip generally performs well in terms of compression ratio and speed, but it may not be the best option for certain types of files. For example, if you need to compress a large number of small files, it may be more efficient to use a tool such as tar that can bundle multiple files into a single archive, and then compress the archive using gzip.

Another thing to consider is that gzip is not the only compression tool available for Linux. Other popular options include bzip2 which is known for its high compression ratio but is slower than gzip, and xz which offers even higher compression ratios than bzip2 but is even slower.

When using gzip, it is important to keep in mind that it is a single-file compression tool, and if you want to compress multiple files together, you should use tar first and then use gzip on the resulting archive.

In conclusion, the gzip command is a widely used and powerful tool that can be used to compress and decompress files in Linux. It uses the Lempel-Ziv coding algorithm, which is known for its high compression ratio and speed. The command has many options and parameters that can be used to customize the compression process, such as specifying the compression level, keeping original file timestamps, and using different compression formats. Additionally, gzip can be used to compress and decompress multiple files at once, making it an essential tool for managing large amounts of data. However, it is important to keep in mind that there are other options available that may be more suitable for certain types of files or situations.