
gzip Command in Linux
gzip is a Linux command that allows you to compress files on the system by reducing their sizes to save the disk space and make the file transfer faster. When you compress a file with the help of gzip utility, it creates a new file with a .gz extension and deletes the original file by default.
The gzip command is commonly used to compress text files, web pages and log. However, it is not an ideal choice for files that are compressed already, such as images or videos.
Table of Contents
Here is a comprehensive guide to the options available with the gzip command −
Syntax of gzip Command
The basic syntax to use the gzip command in Linux is as follows −
gzip [options] [filename]
Where,
- gzip invokes the command to compress the files.
- [options] are optional flags you can use to modify the behavior of the
- [filename] is the name of the file you want to compress or decompress.
Options gzip Command
The following are some few options that can be used with the gzip command in Linux −
Option | Description |
---|---|
-c, --stdout | Direct the compressed output to standard output (stdout) while retaining the original file intact. |
-d, --document | Decompress the specified file. |
-f, --force | Enforce compression or decompression even if the file already exists or is a symbolic link. |
-h, --help | Show a help message summarizing all available options. |
-k, --keep | Keep the original file after compression. |
-l, --license | Display the software license information. |
-n, --no-name | Avoid storing the original file name and timestamp in the compressed file. |
-N, --name | Preserve the original file name and timestamp within the compressed file. |
-q, --quiet | Suppress all warnings and messages. |
-r, --recursive | Recursively compress files in directories and their subdirectories. |
-S, --suffix=SUF | Use the specified suffix instead of .gz for the compressed file. |
-t, --test | Test the integrity of the compressed file without decompressing it. |
-v, --verbose | Display detailed information about the compression process, including the name and percentage reduction of each file. |
-V, --version | Display the version information of gzip. |
-1, --fast | Set the compression level to the fastest, which is -1. |
-9, --best | Set the compression level to the best, which is -9 |
Examples of gzip Command in Linux
Here are some basic practical examples of gzip command in Linux system −
- Basic Compression
- Keep Original File
- Compress Multiple Files Simultaneously
- Compress to Standard Output
- Change Compression Level
Basic Compression
The basic use of gzip in Linux is to compress a file, which can be done by providing the filename with the command. For example −
gzip file.txt
The above command will compress file.txt to file.txt.gz. This is the simplest use of this command, where it takes a file and compresses it.

Keep Original File
In case you are interested in keeping the original file after compression, just add the -k option with the command gzip. For example −
gzip -k file.txt
Running the above command will compress file.txt to file.txt.gz and keep the original file.txt. This is useful if you want to retain the original file for backup or other purposes.

Compress Multiple Files Simultaneously
If you have multiple files that need compression, you can do that all at once by using the file names one by one with a space between them. For example −
gzip file1.txt file2.txt file3.txt
The above command will compress file1.txt, file2.txt, and file3.txt to file1.txt.gz, file2.txt.gz, and file3.txt.gz. This is pretty useful in case when you have several files that need to be compressed in one go, saving you time and effort.

Compress to Standard Output
In case you want to compress a file and write the output to another file, simply use the -c option followed by the redirection operator. For example −
gzip -c file.txt > outputfile.gz
Once you run the command, it will compress file.txt and write the output to outputfile.gz. This helps when you want to transfer the compressed content to a different file.

Change Compression Level
You can specify a range of compression levels from 1 to 9. For example, to get maximum compression, you would run −
gzip -9 file.txt
This command will compress file.txt with the highest compression ratio, which might take longer but will result in a smaller file size.

Conclusion
The gzip is a versatile Linux utility that enables you to compress files, thereby reducing their sizes to save disk space and improve file transfers. This guide has explored the syntax, various options, and practical examples of using the gzip command.
By mastering the gzip command, you can effectively manage file compression, and make it easier to handle large datasets or archives. Becoming proficient with gzip can significantly enhance your productivity in managing file storage and transfers on a Linux system.