Today you will learn how to compress files in Linux using gzip command.
What is File Compression?
File compression is a method by which we can save disk space by reducing the size of available data. A compressed file is a collection in which one or more files are stored.
Why do we need to Compress a file or directory?
Compressed files have many advantages. Here I have explained some of them:
- The compressed file uses less Harddisk space.
When you plan to send multiple files via email (assuming there are 100 files), it is not the right way to attach each file individually as an email attachment and This may exceed the attachment size allowed by your email administrator.
Hence always you should send such files in a compressed way which not only reduces the size of the file but you can send all those files in an organized way.
- When we transfer naked files through the Internet, the chances of it getting corrupted increases. This is why you should always compress and transfer your important files.
We have several ways in Linux by which you can compress files and directories. The most popular tools are:
- gzip
- bzip2
- zip
Suggested Read: Bzip2 Command in Linux with Examples
In this guide, we are going to discuss the complete features of gzip command with examples.
gzip is the most popular compression tool among other similar tools used to compress files only.
But by combining it with the tar command
, you can also compress directories.
Suggested Read: Tar Command in Linux with Examples
Table of Contents
Features of gzip Command:
- Compress and Decompress (Extract) a File
- Reduces file size
- Display the contents of a compressed file
- Tests compressed file integrity
- Lists each compressed File
How to identify a gzip file?
You can identify the gzip
file by its extension. The file extension of gzip
is .gz
First of all, let’s focus on some of the most important options that we can use with the gzip
command.
Options | Explanation |
---|---|
-c, --stdout | Write on standard output, keep original files unchanged |
-d, --decompress | Decompress (Extract a File) |
-f, --force | Force overwrite of output file and compress links |
-h, --help | Display the Help page of Gzip Command |
-k, --keep | Keep (don't delete) input files |
-l, --list | List compressed file contents |
-L, --license | Display software license Information |
-n, --no-name | Do not save or restore the original name and time stamp |
-N, --name | Save or restore the original name and time stamp |
-q, --quiet | Suppress all warnings |
-r, --recursive | Operate Recursively on Directories |
-S, --suffix=SUF | Use suffix SUF on compressed files |
-t, --test | test compressed file integrity |
-v, --verbose | Verbose mode |
-V, --version | Display version number |
-1, --fast | Compress Faster |
-9, --best | Compress Better |
--rsyncable | Make rsync-friendly archive |
Syntax:
You have to follow the below syntax to use gzip
command.
gzip [OPTION]... [FILE]...
1. Compress a File
To compress a file using gzip
type the following command.
Here I am compressing a file named file.txt
.
~$ gzip file.txt
Output:
~$ ls
file.txt.gz

2. Compress multiple Files
You can compress multiple files using gzip
.
To compress multiple files, pass the file names to gzip
. Here I am compressing files named file.txt
, file1.txt
, file2.txt
, file3.txt
, file4.txt
~$ gzip file.txt file1.txt file2.txt file3.txt file4.txt
Output:
~$ ls
file1.txt.gz file2.txt.gz file3.txt.gz file4.txt.gz file.txt.gz

You can also use wildcards with gzip
. Let’s take some examples:
Ex. # 1 Compress those files whose extension is “.txt“.
~$ gzip *.txt
Ex. # 2 Compress those files that start with “fi“.
~$ gzip fi*
3. Keep (Don’t delete) input Files
gzip
by default deletes the input file after compressing.
But if you want to keep the input files during compression, then pass the -k
option to gzip
.
~$ gzip -k file.txt
Output:
~$ ls
file.txt file.txt.gz
By the way, the -c
option helps to concatenate the contents of multiple files.
But you can also use this option to keep input files while compressing.
Here is an example.
Syntax:
gzip -c [INPUT FILE] > [OUTPUT FILE]
~$ gzip -c file.txt > newfile.txt.gz
Now type the following command to concatenate the contents of the two files.
Here in this example, I am concatenating the contents of file.txt
and file1.txt
.
~$ gzip -c file1.txt >> newfile.txt.gz
Now to decompress the file type the following command.
~$ gzip -d newfile.txt.gz
Output:
~$ ls
newfile.txt
~$ cat newfile.txt
Linux is a Open Source Operating System.
I Love Linux
4. Compress all files in a Directory Recursively
To compress all files present in a directory Recursively pass the -r
option to gzip
command.
Here are some files inside the directory named data.
~/data$ ls
file1.txt file2.txt file3.txt file4.txt file5.txt file.txt
Now to compress all these files type the following command.
~$ gzip -r data/
Output:
~$ ls data/
file1.txt.gz file2.txt.gz file3.txt.gz file4.txt.gz file5.txt.gz file.txt.gz
5. Test compressed file integrity
To test the compressed file integrity pass -t
option to gzip
Command.
You must be wondering what is this integrity.
Integrity means that we can check whether a Particular .gz
file is valid or not.
Now you might be wondering what is the need to check validity.
Let me show you an example.
What if I create a .gz
file using mv
command. Refer to the following command.
~$ mv file.txt file.txt.gz
Now let’s check the integrity of this file.
Output:
~$ gzip -t file.txt.gz
gzip: file.txt.gz: unexpected end of file
As you can see on the output above we received an error that “unexpected end of file” because we have not created this file using gzip command.
A valid .gz
file will display the following output.
~$ gzip -tv file1.txt.gz
file.txt.gz: OK
6. List each compressed File
To List, each compressed file pass the -l
option to gzip
.
~$ gzip -l file.txt.gz
compressed uncompressed ratio uncompressed_name
29 0 0.0% file.txt
To get the more detailed information you can combine -l
with -v
. Refer to the following example.
~$ gzip -lv file.txt.gz
method crc date time compressed uncompressed ratio uncompressed_name
defla 00000000 Jun 30 07:03 29 0 0.0% file.txt
7. Decompress (Extract) a File
To Decompress (Extract) a .gz
file pass the -d
option to gzip
.
~$ gzip -d file.txt.gz
OR you can use the gunzip
command to extract a .gz
file.
~$ gunzip file.txt.gz
8. Decompress multiple files
To decompress multiple files, pass the file names to gzip
. Here I am decompressing files named file1.txt
, file2.txt
, file3.txt
, file4.txt
, file.txt
.
~$ gzip -d file1.txt.gz file2.txt.gz file3.txt.gz file4.txt.gz file.txt.gz
You can also use wildcards with gzip
. Let’s take some examples:
Ex. # 1 Decompress those files whose extension is “.gz“
~$ gzip -d *.gz
Ex. # 2 Decompress those files that start with “fi“
~$ gzip -d fi*
9. Overwrite existing output files Forcefully
gzip
command allows us to Overwrite existing output files forcefully.
To do so pass the -f
option to gzip
command.
Let’s take an example.
Overwrite existing output file while Compressing:
I have a file named file.txt
and Compressed version of this file is file.txt.gz
~$ ls
file.txt file.txt.gz
For some reason again I want to compress the same file using gzip
. So Let’s do it.
~$ gzip file.txt
gzip: file.txt.gz already exists; do you wish to overwrite (y or n)?
As you can see on the output above, Command is asking for permission to Overwrite the existing file.
Here you can enter y
to Overwrite the file.
OR If you want proceed further without such notice you can use -f
option.
~$ gzip -f file.txt
Overwrite existing output file while decompressing:
Similarly, Overwrite existing output file while decompressing using -f
option.
~$ gzip -df file.txt.gz
10. gzip Command Compression Level’s
gzip
allows to compress files using several compression levels which is ranges from 1 to 9.
If you do not specify a level with the gzip
command then it uses compressed level -6
to compress files.
Compress Faster:
You can use -1
for the fastest compression speed with a lesser compression ratio.
~$ gzip -1 file.txt
Compress Better:
You can use -9
for lowest compression speed with maximum compression ratio.
~$ gzip -9 file.txt
11. Display output of a Command
To display the verbose output of the command pass the -v
option to gzip
.
Example # 1
~$ gzip -v file.txt
file.txt: 2.4% -- replaced with file.txt.gz
Example # 2
~/data$ gzip -dv file.txt.gz
file.txt.gz: 2.4% -- replaced with file.txt
12. Display the contents of a Compressed File
You can display the contents of a compressed file using the zcat
command.
Let’s take an example:
I have a file named file.txt
with some content. To display the contents of this file you can use the cat
command.
For more information about cat command, see our guide:
~$ cat file.txt
Linux is a Open Source Operating System.
Let’s compress the file using gzip
command.
~$ gzip file.txt
Now to display the contents of file.txt.gz
using zcat
type the following command.
~$ zcat file.txt.gz
Linux is a Open Source Operating System.
Type the following command to display the contents of multiple compressed files.
In this example, i am displaying the content of files named file1.txt.gz
and file.txt.gz
~/data$ zcat file1.txt.gz file.txt.gz
I Love Linux
Linux is a Open Source Operating System.
13. Display the Help page of gzip Command
Use the following commands to access the Manual Page/Help Page of gzip
command.
~$ gzip --help
~$ man gzip
14. Display Software License
To display Software License information of gzip
command use -L
option.
~$ gzip -L
15. Display Version
Check the gzip
command version using the following command.
~/data$ gzip -V
You can visit at following websites to get more information on gzip
.
Infographic
Refer to this Infographic for complete gzip
command options.

Conclusion
I hope you have learned something from this article.
I have tried my best to include all the features of gzip
command in this guide.
Now I’d like to hear your thoughts.
Was this guide useful to you?
Or maybe you have some queries.
Have I not included any command in this guide?
Leave a comment below.