gzip: Compress Files on Linux, Decompress gzip Files, and Use gunzip and zcat

Test environmentRHEL familyAlmaLinux 9.8root accountChecked 2026-11-05
Compression · archive commandsgzipgunzipzcatLocation/usr/bin/gzip
Compressing files on Linux and decompressing gzip files

gzip is the Linux command that compresses a single file into a .gz file; to decompress, add -d to the same command or use gunzip. On an AlmaLinux 9 server, a 31,878-byte access log shrank to 4,424 bytes, and decompressing it brought back the original size and modification time.

The output below comes from a test folder on an AlmaLinux 9.8 server, run as root, with times in UTC. The access logs, notes and configuration files are sample files made for the test, and the IP addresses in the logs are documentation example values.

1️⃣ How to use the gzip command

  • ↪️ Compressing a file and keeping the original (-k)

The test folder holds two access logs, two note files, a config folder with configuration files and an empty restore folder.

ls -l
total 88
-rw-r--r-- 1 root root 31878 Oct 28 23:50 access.log
-rw-r--r-- 1 root root 47748 Oct 27 23:59 access.log-20261027
drwxr-xr-x 2 root root    39 Oct 29 00:19 config
-rw-r--r-- 1 root root    45 Oct 28 14:30 memo.txt
-rw-r--r-- 1 root root   426 Oct 27 09:10 readme.txt
drwxr-xr-x 2 root root     6 Oct 29 00:19 restore

When you put a file name after gzip, that file turns into a compressed file with .gz added to the end of its name. The command below compresses access.log and prints nothing when it finishes.

gzip access.log

After compression, ls -l shows no access.log, only access.log.gz. The 31,878-byte log became 4,424 bytes, and the modification time is still that of the original file.

ls -l access.log access.log.gz
ls: cannot access 'access.log': No such file or directory
-rw-r--r-- 1 root root 4424 Oct 28 23:50 access.log.gz

With -v, gzip prints the reduction ratio and the new file name for each file. The [81.5%] on the readme.txt line is how much smaller the file got, and [replaced with] means the original file was replaced by the .gz file.

gzip -v readme.txt
readme.txt:	 81.5% -- replaced with readme.txt.gz

To keep the original file, add -k. Compressing memo.txt with -k leaves memo.txt and memo.txt.gz side by side.

gzip -k memo.txt
ls -l memo.txt memo.txt.gz
-rw-r--r-- 1 root root 45 Oct 28 14:30 memo.txt
-rw-r--r-- 1 root root 71 Oct 28 14:30 memo.txt.gz

The 45-byte memo.txt actually grew to 71 bytes after compression. According to the official manual, gzip compresses even when the result is larger than the original, and it stores the original file name and time inside the compressed file.

If you list several files at once, each file gets its own .gz file. Compressing the two configuration files in the config folder produces two files, app.conf.gz and site.conf.gz.

gzip -v config/app.conf config/site.conf
config/app.conf:	 78.7% -- replaced with config/app.conf.gz
config/site.conf:	 87.7% -- replaced with config/site.conf.gz
ls -l config/
total 8
-rw-r--r-- 1 root root 206 Oct 20 11:00 app.conf.gz
-rw-r--r-- 1 root root  93 Oct 20 11:05 site.conf.gz

2️⃣ Decompressing gzip files

  • ↪️ gzip -d and gunzip, decompressing to another folder (-c)

Decompressing access.log.gz with -d prints nothing, access.log comes back, and access.log.gz is gone.

gzip -d access.log.gz
ls -l access.log access.log.gz
ls: cannot access 'access.log.gz': No such file or directory
-rw-r--r-- 1 root root 31878 Oct 28 23:50 access.log

The restored access.log is 31,878 bytes, and its modification time is the same Oct 28 23:50 as before compression.

gunzip does the same job as gzip -d. With -k it decompresses while keeping the compressed file, and [created] on the -v line means it made a new file without deleting the .gz file.

gunzip -kv readme.txt.gz
readme.txt.gz:	 81.5% -- created readme.txt
ls -l readme.txt readme.txt.gz
-rw-r--r-- 1 root root 426 Oct 27 09:10 readme.txt
-rw-r--r-- 1 root root 108 Oct 27 09:10 readme.txt.gz

gzip has no option for choosing the output folder, so to decompress into another folder, use -c to send the decompressed data to standard output and put the new file path after [>]. The command below decompresses memo.txt.gz into memo.txt in the restore folder.

gzip -dc memo.txt.gz > restore/memo.txt
ls -l restore/
total 4
-rw-r--r-- 1 root root 45 Oct 29 00:19 memo.txt

Decompressing with -c leaves memo.txt.gz in place. The new restore/memo.txt has a modification time of Oct 29 00:19, when the file was created.

3️⃣ gzip command options

  • ↪️ -l, -t, compression level, -r

-l prints, on one line and without decompressing, the compressed size, the original size, the reduction ratio and the name the file gets when decompressed. After compressing access.log again with -k, -l shows the following.

gzip -k access.log
gzip -l access.log.gz
         compressed        uncompressed  ratio uncompressed_name
               4424               31878  86.2% access.log

Adding -v puts the compression method (defla, short for deflate), the CRC of the original data and the date and time of the original file in front.

gzip -lv access.log.gz
method  crc     date  time           compressed        uncompressed  ratio uncompressed_name
defla b49b41c4 Oct 28 23:50                4424               31878  86.2% access.log

-t only checks whether the compressed file is intact and then exits. Used with -v, it prints [OK] after each file without problems.

gzip -tv access.log.gz
access.log.gz:	 OK

The numbers -1 to -9 set the compression level, and according to the official manual the default is -6. The same access.log was compressed three times with -c and the sizes were compared with wc -c.

gzip -c -1 access.log | wc -c
5564
gzip -c -6 access.log | wc -c
4424
gzip -c -9 access.log | wc -c
4164
Compression level.gz sizeOf original size
Original file (access.log)31,878 bytes100%
-1 (fastest)5,564 bytes17.5%
-6 (default)4,424 bytes13.9%
-9 (smallest)4,164 bytes13.1%

-9 was 260 bytes smaller than the default, and -1 was 1,140 bytes larger than the default.

If you give only a folder name, gzip prints [is a directory -- ignored] and skips the folder.

gunzip config
gzip: config is a directory -- ignored

To process every file inside a folder, add -r. Running gunzip -r on the config folder, where the two configuration files had been compressed, decompresses both files one after another.

gunzip -rv config
config/app.conf.gz:	 78.7% -- replaced with config/app.conf
config/site.conf.gz:	 87.7% -- replaced with config/site.conf
ls -l config/
total 8
-rw-r--r-- 1 root root 840 Oct 20 11:00 app.conf
-rw-r--r-- 1 root root 528 Oct 20 11:05 site.conf

-r compresses or decompresses the files in a folder one by one and does not bundle several files into one. To turn a folder into a single compressed file, use the tar command to bundle and compress it in one step. Compressing the config folder with tar -czf puts the folder and both files into a single config.tar.gz.

tar -czf config.tar.gz config
tar -tzvf config.tar.gz
drwxr-xr-x root/root         0 2026-10-29 00:19 config/
-rw-r--r-- root/root       840 2026-10-20 11:00 config/app.conf
-rw-r--r-- root/root       528 2026-10-20 11:05 config/site.conf

📌 Main options: results from this article

OptionTaskResult in this article
-cSends the result to standard output and leaves the original file as it isSending the output of -dc to restore/memo.txt created a 45-byte memo.txt
-dDecompress (same as gunzip)access.log.gz disappears and the 31878-byte access.log comes back
-kKeeps the original file after compressing or decompressingmemo.txt (45 bytes) and memo.txt.gz (71 bytes) remain side by side
-vPrints the reduction ratio and new file name for each filereadme.txt: 81.5% -- replaced with readme.txt.gz
-lLists the size, ratio and decompressed name of compressed filesCompressed size 4424, original size 31878, ratio 86.2%, name access.log
-tChecks whether the compressed file is intact-tv gives access.log.gz: OK, and unexpected end of file for broken.gz, whose end was cut off
-rCompresses or decompresses every file inside a foldergunzip -rv config decompresses config/app.conf.gz and config/site.conf.gz in one go
-fOverwrites a file with the same name without askingOverwritten without a prompt, leaving no memo.txt and only memo.txt.gz (71 bytes)
-1 ~ -9Compression level (-1 fastest, -9 smallest, default -6)Counting the -1, -6 and -9 outputs with wc -c gave 5564, 4424 and 4164

📎 Other options: per the official manual

OptionTask
-NStores the original file name and time when compressing (default for compression) and restores the stored name and time when decompressing
-nDoes not store the original file name and time when compressing, and uses the name minus .gz when decompressing (default for decompression)
-qSuppresses all warnings
-S SUFFIXCompresses with the given suffix instead of .gz, and looks for that suffix first when decompressing
--rsyncableCompresses so that only the changed parts are sent when syncing compressed files with another server using rsync

🔲 gzip command simulator

In the simulator below, you can type the commands yourself to compress with gzip, decompress, and view the contents with zcat and zgrep.

gzip command simulatorgzip 1.12
[root@server work]#
It runs only in your browser and does not actually compress any files. Compressed sizes are measured values, and content newly created in the simulator uses estimated values. Use ↑↓ to recall earlier commands.

4️⃣ Viewing gzip file contents

  • ↪️ zcat · zgrep

Old logs saved with a date in their name take up less space once compressed with gzip. The command below compresses access.log-20261027, the log from the previous day.

gzip access.log-20261027

A compressed log can be printed to the screen with zcat without decompressing it. zcat is the same as gunzip -c (according to the official manual), so the .gz file stays as it is.

zcat access.log-20261027.gz | head -3
192.0.2.50 - - [27/Oct/2026:00:00:00 +0000] GET / HTTP/1.1 200 15929
192.0.2.54 - - [27/Oct/2026:00:00:07 +0000] GET / HTTP/1.1 200 17699
192.0.2.4 - - [27/Oct/2026:00:00:14 +0000] GET / HTTP/1.1 200 13813

The output of zcat can be piped to other commands, and counting with wc -l shows that this log has 600 lines.

zcat access.log-20261027.gz | wc -l
600

To search for specific text in a compressed log, use zgrep. With -c, it prints only the number of lines containing [ 404 ]; to see the lines themselves, run it without -c.

zgrep -c ' 404 ' access.log-20261027.gz
37
zgrep ' 404 ' access.log-20261027.gz | head -3
192.0.2.23 - - [27/Oct/2026:00:02:06 +0000] GET /old-page-6/ HTTP/1.1 404 229
192.0.2.38 - - [27/Oct/2026:00:04:40 +0000] GET /old-page-2/ HTTP/1.1 404 217
192.0.2.22 - - [27/Oct/2026:00:07:42 +0000] GET /old-page-6/ HTTP/1.1 404 414

When zcat gets a file that is not compressed, it prints [not in gzip format] and stops.

zcat access.log | head -1

gzip: access.log: not in gzip format

zgrep also reads uncompressed files as they are, so you can search the current log and the compressed old logs in one go. With two or more files, each line count is prefixed with the file name.

zgrep -c wp-login.php access.log access.log-20261027.gz
access.log:36
access.log-20261027.gz:52

5️⃣ gzip errors

  • ↪️ not in gzip format and unexpected end of file

Compressing a file that already ends in .gz prints [already has .gz suffix -- unchanged] and leaves the file as it is.

gzip access.log.gz
gzip: access.log.gz already has .gz suffix -- unchanged

Conversely, decompressing a file that does not end in .gz with -d prints [unknown suffix -- ignored].

gzip -d memo.txt
gzip: memo.txt: unknown suffix -- ignored

A file that is only named .gz but was not compressed with gzip ends with [not in gzip format]. This message appeared when the text file memo.txt was copied as notgzip.gz and decompressed.

cp memo.txt notgzip.gz
gzip -d notgzip.gz

gzip: notgzip.gz: not in gzip format

A .gz file whose end was cut off, for example by an interrupted download, ends with [unexpected end of file]. This is the result of checking broken.gz, which kept only the first 2,000 bytes of a compressed old log, with -t.

head -c 2000 access.log-20261027.gz > broken.gz
gzip -t broken.gz

gzip: broken.gz: unexpected end of file

Even from a truncated file, zcat can recover the content up to the point where it was cut. Of the 600-line log, 162 lines were left in restore/broken.log, and the official manual also recommends using zcat to rescue the beginning of a damaged compressed file.

zcat broken.gz > restore/broken.log
wc -l restore/broken.log

gzip: broken.gz: unexpected end of file
162 restore/broken.log

If a file with the same name as the .gz file to be created already exists, gzip asks whether to overwrite it. Typing n ends the command with [not overwritten] and changes nothing.

gzip memo.txt
gzip: memo.txt.gz already exists; do you wish to overwrite (y or n)? n
	not overwritten

With -f, gzip overwrites without asking. Because -k was not added, memo.txt disappears and only memo.txt.gz remains.

gzip -f memo.txt
ls -l memo.txt memo.txt.gz
ls: cannot access 'memo.txt': No such file or directory
-rw-r--r-- 1 root root 71 Oct 28 14:30 memo.txt.gz

The error messages that came up while working with compression, and what to check next, are as follows.

OutputMeaningWhat to check next
already has .gz suffix -- unchangedThe file already ends in .gz, so it is not compressed againWhether you typed the name of the original file to compress
unknown suffix -- ignoredTried to decompress a file that does not end in .gzThe .gz file name with ls
not in gzip formatNamed .gz but not compressed with gzipThe actual format (zip, tar) and the matching command
unexpected end of fileThe end of the compressed file is cut offCompare the size with the original and download again · rescue the beginning with zcat
already exists; do you wish to overwriteA file with the same name as the one to be created already existsy or -f if overwriting is fine · -c with another name to keep it
is a directory -- ignoredGave a folder name instead of a file-r for the files in a folder · tar to bundle them into one

🔢 FAQ & Recommended Content

After bundling the config folder into x.tar with tar, compressing it with gzip -v and decompressing it with gunzip -v, only the .gz layer was removed, leaving a 10,240-byte x.tar. To get the bundled files out, you need to unpack it once more with tar. When x.tar.gz was compressed again and extracted into the restore/t folder with tar -xzf, the config folder and both configuration files came out in one step.

A 28,648,800-byte file made by joining the same access log 600 times was compressed twice at each level. -1 took about 0.3 seconds for 4,821,317 bytes, the default -6 about 0.65 seconds for 3,625,500 bytes, and -9 about 2.5 seconds for 3,376,224 bytes. -9 was about 7% smaller than -6 but took almost four times as long.

gzip stores the original file name inside the .gz file when compressing. After readme.txt.gz was copied to the restore folder as backup-1.gz, gzip -lN showed the stored name readme.txt, and gunzip -N created restore/readme.txt. Without -N, the name is the file name minus .gz (according to the official manual).

ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.

Leave a Reply

Your email address will not be published. Required fields are marked *

Prove your humanity: 2   +   7   =