In this quick guide, we will explore the gzip
command, which is used to compress files using the LZ77 compression protocol. Compressing files can help reduce their size and save storage space. Let’s dive into the various options and features of gzip
.
Compressing a File
The simplest way to compress a file is by using the following command:
gzip filename
This command will compress the specified file and append the .gz
extension to it. By default, the original file is deleted during compression. To preserve the original file, you can use the -c
option along with output redirection, like this:
gzip -c filename > filename.gz
Using the -c
option ensures that the output is redirected to the standard output stream while leaving the original file intact.
Alternatively, you can use the -k
option, which creates a compressed file without deleting the original:
gzip -k filename
Compression Levels
gzip
provides various levels of compression. The higher the compression level, the more time it takes to compress and decompress the file. The levels range from 1 (fastest, worst compression) to 9 (slowest, better compression), with the default level being 6.
To choose a specific compression level, you can use the -<NUMBER>
option. For example, to set the compression level to 1:
gzip -1 filename
Compressing Multiple Files
To compress multiple files, you can provide a list of filenames as arguments to the gzip
command. For instance:
gzip filename1 filename2
This command will compress each specified file individually.
You can also compress all the files in a directory and its subdirectories recursively by using the -r
option. For example:
gzip -r a_folder
Compression Percentage Information
The -v
option can be used to display compression percentage information. It is often used together with the -k
option to keep the original file. Here’s an example:
Image: Compression Percentage Information Screenshot
Decompressing a File
In addition to compression, gzip
can also decompress files. Use the -d
option followed by the filename with the .gz
extension to decompress a file. Here’s an example:
gzip -d filename.gz
Compatibility
The gzip
command works on Linux, macOS, WSL (Windows Subsystem for Linux), and anywhere you have a UNIX environment. You can use it across these platforms to compress and decompress files efficiently.