×

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.


Related Topics

scp Command in Linux/Unix with Examples

The scp (secure copy) command in Linux is a command-line utility that is used to securely transfer files between two hosts over a network. It is similar to the cp...

4 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

cp Command in Linux/Unix with Examples

The cp (copy) command in Linux is a command-line utility that is used to copy files and directories. It is one of the most basic and commonly used commands in...

5 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

Linux apt-get command

Apt-get is a command-line that helps to handle the packages in Linux. Its major task is to assist in retrieving data and packages from trusted sources for purposes of installation,...

6 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

4 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes read.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

4 minutes read.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.

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...

6 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.