×

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

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files...

4 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

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.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

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

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.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

unzip Command in Linux/Unix with Examples

The 'unzip' command in Linux is used to extract files from a ZIP archive file. The command allows you to decompress and extract the files and directories stored in a...

3 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' 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...

3 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

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.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.