Introduction
Gzip is a popular and powerful command line tool used to compress and decompress files in Linux. It is a widely used tool for archiving and compressing files, and is the default choice for many Linux distributions. In this article, we will discuss 10 practical examples of using the gzip command in Linux. We will cover how to compress and decompress files, as well as how to use gzip to create and extract archives. We will also discuss some of the more advanced features of gzip, such as setting compression levels and using gzip to create password-protected archives. By the end of this article, you should have a good understanding of how to use gzip in Linux.
10 Practical Examples of Using the Gzip Command in Linux
1. Compress a single file:
gzip filename.txt
2. Compress multiple files:
gzip file1.txt file2.txt file3.txt
3. Compress all files in a directory:
gzip *
4. Compress a directory and its contents:
gzip -r directoryname
5. Compress a file and keep the original:
gzip -k filename.txt
6. Compress a file and rename it:
gzip -c filename.txt > filename.gz
7. Decompress a file:
gunzip filename.gz
8. Decompress multiple files:
gunzip file1.gz file2.gz file3.gz
9. Decompress a file and keep the original:
gunzip -k filename.gz
10. Decompress a file and rename it:
gunzip -c filename.gz > filename.txt
Compression is a very commonly performed operation by users to save disk space as well as reduce time and bandwidth while transferring large amounts of data over the network using gzip utility.
gzip stands for the GNU zip and it is a very popular compression and decompression utility. One of the primary reasons for its popularity is its high compression ratio and speed, which means the compressed data remains the same after decompressing.
The gzip command uses a deflate algorithm which is a lossless data compression that creates smaller file size to make file transfer much faster, as compared to other compression tools.
In this guide, we are going to discuss gzip command usage with examples in Linux.
gzip Command Syntax
Just like any other Linux command, the gzip command’s syntax is divided into two parts OPTIONS and FILES.
$ gzip [OPTIONS]... [FILES]...
In the above syntax, the OPTIONS are used to alter the behavior of the command whereas the FILES represent the input files.
1. Create a Gzip File in Linux
One of the very common uses of the gzip command is to compress a large file. It is very common to see that, large ISO files or tar bundles are compressed to save disk space.
To compress a single file, we just need to pass the file name to the gzip command. To understand this, let’s use the following command to compress the ISO file:
$ gzip alma-linux.iso
Now, let’s use the ls command to verify that the file has been compressed:
$ ls -l
In the above output, we can see the new compressed file with the name alma-linux.iso.gz. We can also observe that, by default, the gzip command adds a .gz
extension to a compressed file.
2. Create Gzip and Keep Original File
In the previous example, we saw how easy it is to compress a file. However, if we observe carefully then we can notice that the gzip command deletes the original file after compressing it.
However, sometimes we want to keep the original file as well. In such cases, we can use the -k
option as shown.
$ gzip -k alma-linux.iso $ ls -l
In the above output, we can see that the original file is intact even after the compression.
3. View Contents of a .gz File
To view the contents of a compressed .gz
file, use the zcat command – which allows you to view the compressed file contents without uncompressing.
$ zcat alma-linux.iso
4. View Info of a .gz File
Sometimes, we want to display more details about the compressed file. In such a case, we can use the -l
option to list the following fields:
- compressed size – it represents the size of the compressed file in bytes.
- uncompressed size – it represents the size of the uncompressed file in bytes.
- ratio – it represents the compression ratio.
- uncompressed_name – it represents the name of the uncompressed file.
To understand this, let’s execute the following command:
$ gzip -l alma-linux.iso.gz
5. Overwrite Gzip File Without Confirmation
By default, the gzip command works in an interactive way if the compressed file with the same name already exists. To understand this default behaviour, let’s execute the same command from the previous example:
$ gzip -k alma-linux.iso
Here, we can see that the gzip command waits for the user input. Now, we can use the 'y'
to overwrite the file or 'n'
to abort the operation.
This interactive operation is safe and avoids overwriting files by mistake. However, this is not suitable every time. For example, if we are executing the gzip command from the script then the script will wait infinitely for the user input. In such cases, we can use the -f
option which overwrites the files forcefully.
Now, let’s execute the same command with the -f
option:
$ gzip -f -k alma-linux.iso
Here, we can see that now the gzip command works in a non-interactive way.
6. Compress Multiple Files with Gzip
So far we saw how to compress a single file. In a similar way, we can use the gzip command to compress multiple files at once.
So, first, let’s create multiple copies of the file using the following cp command:
$ cp alma-linux.iso alma-linux-1.iso $ cp alma-linux.iso alma-linux-2.iso $ cp alma-linux.iso alma-linux-3.iso
Next, let’s compress the three files using the below command:
$ gzip alma-linux-1.iso alma-linux-2.iso alma-linux-3.iso $ ls -l
7. Gzip All Files in a Directory
In the previous example, we saw how to compress multiple files. In a similar way, we can also compress all the files from a directory.
To understand this, first let us create a new directory and add a few files into it:
$ mkdir dir-1 $ touch dir-1/file-1.txt dir-1/file-2.txt dir-1/file-3.txt
Now, let’s use the -r
option to compress all the files from the dir-1 directory:
$ gzip -r dir-1/ $ ls -l dir-1/
In the above example, the -r
option traverses the directory in a recursive fashion.
8. Decompress a Gzip File in Linux
The gzip command allows us to decompress the file using the -d
option as shown.
$ gzip -d alma-linux.iso.gz $ ls -l
9. Compress Tar File to Gzip
In the previous two examples, we saw how to compress multiple files using a single command. However, we can also observe that the gzip command doesn’t compress these files into a single file. In such cases, first, we can create a tar bundle and then compress it using the gzip command.
So, first, let’s create a tar bundle with multiple files in it:
$ tar cvf sample.tar alma-linux-1.iso alma-linux-2.iso
Now, let us compress this tar bundle using the following command:
$ gzip sample.tar $ ls -l
10. Speeding Up gzip Compression
So far, we used very small files to demonstrate the usage of the gzip command. However, in real scenarios, the data that needs to be compressed can be really large.
In such cases, we can use the --fast
option to reduce the compression time.
$ gzip --fast alma-linux.iso
It is important to note that, the --fast
option gives preference to the compression speed over the ratio.
11. Speeding Up Gzip Compression Ratio
In a similar way, we can use the --best
option to improve the compression ratio. To understand this, let’s execute the below command:
$ gzip --best alma-linux.iso
Here, we should note that the --best
option gives preference to the compression ratio over the speed.
12. Set Gzip Compression Level
We can use an integer argument with the gzip command to regulate the speed of the compression. The valid value for the range is between 1 to 9. The value 1 represents the fasted compression whereas the value 9 represents the slowest compression.
For example, the following command uses the 2 as an argument to improve the compression speed:
$ gzip -2 alma-linux.iso
It is important to note that, the default compression level in gzip is -6. It prefers high compression at the expense of speed.
13. Change Gzip File Extension Suffix
By default, the gzip command uses the .gz
suffix after compressing the file. However, we can override this default behavior using the --suffix
option.
For example, we can use the below command to use gnuzip as a suffix:
$ gzip --suffix .gnuzip alma-linux.iso $ ls -l
In the above example, we can see the compressed file has a .gnuzip
extension.
Conclusion
In this practical guide, we discussed some of the common examples of the gzip command in Linux to compress and decompress files faster.
Do you know of any other best example of the gzip command in Linux? Let us know your views in the comments below.
10 Practical Examples of Using the Gzip Command in Linux
Gzip is a popular file compression tool in Linux. It is used to reduce the size of a file or a group of files in order to save disk space and to make it easier to transfer them over the network. In this article, we will discuss 10 practical examples of using the Gzip command in Linux.
1. Compress a Single File
The most basic usage of the Gzip command is to compress a single file. To do this, you can use the following command:
gzip filename
This will create a compressed file with the same name as the original file, but with the .gz extension. For example, if you compress a file named “example.txt”, the compressed file will be named “example.txt.gz”.
2. Compress Multiple Files
You can also use the Gzip command to compress multiple files at once. To do this, you can use the following command:
gzip file1 file2 file3
This will create compressed versions of each of the files, with the same name as the original files, but with the .gz extension.
3. Compress an Entire Directory
You can also use the Gzip command to compress an entire directory. To do this, you can use the following command:
gzip -r directory_name
This will create compressed versions of all the files in the directory, with the same name as the original files, but with the .gz extension.
4. Compress a File and Preserve the Original
By default, the Gzip command will delete the original file after creating the compressed version. If you want to keep the original file, you can use the -k (or –keep) option. For example:
gzip -k filename
This will create a compressed version of the file, with the same name as the original file, but with the .gz extension, and it will keep the original file.
5. Compress a File and Change the Name
By default, the Gzip command will create a compressed file with the same name as the original file, but with the .gz extension. If you want to change the name of the compressed file, you can use the -c (or –stdout) option. For example:
gzip -c filename > new_filename.gz
This will create a compressed version of the file, with the name “new_filename.gz”.
6. Compress a File and Print the Output
By default, the Gzip command will create a compressed file and save it to disk. If you want to print the output to the terminal instead, you can use the -t (or –test) option. For example:
gzip -t filename
This will print the compressed version of the file to the terminal.
7. Compress a File and Show the Compression Ratio
By default, the Gzip command will create a compressed file without showing the compression ratio. If you want to see the compression ratio, you can use the -l (or –list) option. For example:
gzip -l filename
This will show the compression ratio of the file.
8. Compress a File and Show the Progress
By default, the Gzip command will create a compressed file without showing the progress. If you want to see the progress, you can use the -v (or –verbose) option. For example:
gzip -v filename
This will show the progress of the compression.
9. Decompress a File
If you have a compressed file, you can use the Gzip command to decompress it. To do this, you can use the following command:
gunzip filename.gz
This will create a decompressed version of the file, with the same name as the original file.
10. Decompress Multiple Files
You can also use the Gzip command to decompress multiple files at once. To do this, you can use the following command:
gunzip file1.gz file2.gz file3.gz
This will create decompressed versions of each of the files, with the same name as the original files.
These are just some of the practical examples of using the Gzip command in Linux. For more information, you can refer to the Gzip man page.