- 13 Practical Examples of Using the Gzip Command in Linux
- gzip Command Syntax
- 1. Create a Gzip File in Linux
- 2. Create Gzip and Keep Original File
- 3. View Contents of a .gz File
- 4. View Info of a .gz File
- 5. Overwrite Gzip File Without Confirmation
- 6. Compress Multiple Files with Gzip
- 7. Gzip All Files in a Directory
- 8. Decompress a Gzip File in Linux
- 9. Compress Tar File to Gzip
- 10. Speeding Up gzip Compression
- 11. Speeding Up Gzip Compression Ratio
- 12. Set Gzip Compression Level
- 13. Change Gzip File Extension Suffix
- Linux tar Command – How to Compress Files in Linux
- What is the tar command?
- How to create an archive
- How to remove files after compression
- How to view the contents of an archive
- How to extract an archive
- How to search in compressed log files
- Wrapping Up
13 Practical Examples of Using the Gzip Command in Linux
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.
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:
Now, let’s use the ls command to verify that the file has been compressed:
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.
To understand this, first, create a simple text file using the redirection operator:
$ echo "zcat example from tecmint.com" > demo $ gzip demo $ ls -l $ zcat demo
In the above output, we can see that the zcat displays the contents of the compressed file without uncompressing it.
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:
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:
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:
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:
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:
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.
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:
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:
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.
Linux tar Command – How to Compress Files in Linux
Zaira Hira
File compression is an essential utility across all platforms. It helps you reduce file size and share files efficiently. And compressed files are also easier to copy to remote servers.
You can also compress older and rarely used files and save them for future use which helps you conserve disk space.
In this post, we’ll look at how to compress files with the tar command in Linux, along with some examples of tar in action.
What is the tar command?
We use the tar command to compress and expand files from the command line. The syntax is shown below:
tar [flags] destinationFileName sourceFileName
The tar command uses the following flags to customize the command input:
Flag | Explanation | Usage |
---|---|---|
-c | Create a new archive. | We use this flag whenever we need to create a new archive. |
-z | Use gzip compression. | When we specify this flag, it means that archive will be created using gzip compression. |
-v | Provide verbose output. | Providing the -v flag shows details of the files compressed. |
-f | Archive file name. | Archive file names are mapped using the -f flag. |
-x | Extract from a compressed file. | We use this flag when files need to be extracted from an archive. |
How to create an archive
We have a list of the following files which we’ll compress with tar .
To compress them, we’ll use tar like this:
tar -czvf logs_archive.tar.gz *
Let’s break down this command and look into each flag.
-c is creating and archive.
-z is using gzip compression.
-v is providing details of the files that have been archived.
-f is creating an archive with the name ‘logs_archive.tar.gz’ as supplied in the command above.
In the results below, we can see that the archive has been created successfully.
How to remove files after compression
Let’s say we don’t want to keep the original files after creating an archive. For that, we can use the —remove-files flag.
tar -czvf logs_archive.tar.gz * --remove-files
Here, the -czvf flags are working as demonstrated before, but the original files are also removed. Once we list the files, we only see the archive.
How to view the contents of an archive
You might need to view the contents of an archive without actually extracting it. You can do this with the -t flag.
tar -tvf logs_archive.tar.gz
In this command, -t flag specifies that we need to only view the contents of the archive. -f specifies the filename and -v displays the detailed contents.
How to extract an archive
To extract files from an archive, you use the -x flag like this:
tar -xzvf logs_archive.tar.gz
Let’s break down this command and look into each flag.
-x is extracting and archive.
-z specifies that the archive is gzip.
-v is providing details of the files that have been archived.
-f is extracting from the archive named ‘logs_archive.tar.gz’.
Here’s a useful tip: commands that take a long time to execute can continue in the background with & .
Adding files to an archive and extracting an archive can take a while. To keep the commands running in the background while you keep working, pair the command with & like this:
tar -xzvf logs_archive.tar.gz &
How to search in compressed log files
You might still need to access certain files once they’re archived. Luckily, there is a method you can use to search and view compressed log files without decompressing them and compromising disk space.
The command you can use to search in compressed files is zgrep :
We can search for a string in an archive using the below command:
zgrep -Hna 'string-to-search' compressedFile.tar.gz
Let’s briefly look at the flags.
-H lists the file name that contains the match.
-n displays the line number that contains the matched string.
-a treats all files as text files.
Wrapping Up
File compression helps us save time and resources when sharing files. Servers are almost always rotating and archiving huge log files.
You can also schedule file compression via cron jobs to automate disk cleaning. I highly recommend that you take advantage of this utility.
Thanks for reading until the end. I would love to connect with you. You can find me here on Twitter. Do share your thoughts.