gzip: Compress Files on Linux, Decompress gzip Files, and Use gunzip and zcat
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
The test folder holds two access logs, two note files, a config folder with configuration files and an empty restore folder.
ls -ltotal 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 restoreWhen 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.logAfter 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.gzls: cannot access 'access.log': No such file or directory
-rw-r--r-- 1 root root 4424 Oct 28 23:50 access.log.gzWith -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.txtreadme.txt: 81.5% -- replaced with readme.txt.gzTo keep the original file, add -k. Compressing memo.txt with -k leaves memo.txt and memo.txt.gz side by side.
gzip -k memo.txtls -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.gzThe 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.confconfig/app.conf: 78.7% -- replaced with config/app.conf.gz
config/site.conf: 87.7% -- replaced with config/site.conf.gzls -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.gz2️⃣ Decompressing gzip files
Decompressing access.log.gz with -d prints nothing, access.log comes back, and access.log.gz is gone.
gzip -d access.log.gzls -l access.log access.log.gzls: cannot access 'access.log.gz': No such file or directory
-rw-r--r-- 1 root root 31878 Oct 28 23:50 access.logThe 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.gzreadme.txt.gz: 81.5% -- created readme.txtls -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.gzgzip 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.txtls -l restore/total 4
-rw-r--r-- 1 root root 45 Oct 29 00:19 memo.txtDecompressing 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 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.loggzip -l access.log.gz compressed uncompressed ratio uncompressed_name
4424 31878 86.2% access.logAdding -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.gzmethod 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.gzaccess.log.gz: OKThe 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 -c5564gzip -c -6 access.log | wc -c4424gzip -c -9 access.log | wc -c4164| Compression level | .gz size | Of original size |
|---|---|---|
| Original file (access.log) | 31,878 bytes | 100% |
| -1 (fastest) | 5,564 bytes | 17.5% |
| -6 (default) | 4,424 bytes | 13.9% |
| -9 (smallest) | 4,164 bytes | 13.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 configgzip: config is a directory -- ignoredTo 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 configconfig/app.conf.gz: 78.7% -- replaced with config/app.conf
config/site.conf.gz: 87.7% -- replaced with config/site.confls -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 configtar -tzvf config.tar.gzdrwxr-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
| Option | Task | Result in this article | |
|---|---|---|---|
| -c | Sends the result to standard output and leaves the original file as it is | Sending the output of -dc to restore/memo.txt created a 45-byte memo.txt | |
| -d | Decompress (same as gunzip) | access.log.gz disappears and the 31878-byte access.log comes back | |
| -k | Keeps the original file after compressing or decompressing | memo.txt (45 bytes) and memo.txt.gz (71 bytes) remain side by side | |
| -v | Prints the reduction ratio and new file name for each file | readme.txt: 81.5% -- replaced with readme.txt.gz | |
| -l | Lists the size, ratio and decompressed name of compressed files | Compressed size 4424, original size 31878, ratio 86.2%, name access.log | |
| -t | Checks 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 | |
| -r | Compresses or decompresses every file inside a folder | gunzip -rv config decompresses config/app.conf.gz and config/site.conf.gz in one go | |
| -f | Overwrites a file with the same name without asking | Overwritten without a prompt, leaving no memo.txt and only memo.txt.gz (71 bytes) | |
| -1 ~ -9 | Compression 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
| Option | Task | |
|---|---|---|
| -N | Stores the original file name and time when compressing (default for compression) and restores the stored name and time when decompressing | |
| -n | Does not store the original file name and time when compressing, and uses the name minus .gz when decompressing (default for decompression) | |
| -q | Suppresses all warnings | |
| -S SUFFIX | Compresses with the given suffix instead of .gz, and looks for that suffix first when decompressing | |
| --rsyncable | Compresses 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.
4️⃣ Viewing gzip file contents
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-20261027A 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 -3192.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 13813The 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 -l600To 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.gz37zgrep ' 404 ' access.log-20261027.gz | head -3192.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 414When 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 formatzgrep 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.gzaccess.log:36
access.log-20261027.gz:525️⃣ gzip errors
Compressing a file that already ends in .gz prints [already has .gz suffix -- unchanged] and leaves the file as it is.
gzip access.log.gzgzip: access.log.gz already has .gz suffix -- unchangedConversely, decompressing a file that does not end in .gz with -d prints [unknown suffix -- ignored].
gzip -d memo.txtgzip: memo.txt: unknown suffix -- ignoredA 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.gzgzip -d notgzip.gz
gzip: notgzip.gz: not in gzip formatA .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.gzgzip -t broken.gz
gzip: broken.gz: unexpected end of fileEven 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.logwc -l restore/broken.log
gzip: broken.gz: unexpected end of file
162 restore/broken.logIf 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.txtgzip: memo.txt.gz already exists; do you wish to overwrite (y or n)? n
not overwrittenWith -f, gzip overwrites without asking. Because -k was not added, memo.txt disappears and only memo.txt.gz remains.
gzip -f memo.txtls -l memo.txt memo.txt.gzls: cannot access 'memo.txt': No such file or directory
-rw-r--r-- 1 root root 71 Oct 28 14:30 memo.txt.gzThe error messages that came up while working with compression, and what to check next, are as follows.
| Output | Meaning | What to check next |
|---|---|---|
| already has .gz suffix -- unchanged | The file already ends in .gz, so it is not compressed again | Whether you typed the name of the original file to compress |
| unknown suffix -- ignored | Tried to decompress a file that does not end in .gz | The .gz file name with ls |
| not in gzip format | Named .gz but not compressed with gzip | The actual format (zip, tar) and the matching command |
| unexpected end of file | The end of the compressed file is cut off | Compare the size with the original and download again · rescue the beginning with zcat |
| already exists; do you wish to overwrite | A file with the same name as the one to be created already exists | y or -f if overwriting is fine · -c with another name to keep it |
| is a directory -- ignored | Gave a folder name instead of a file | -r for the files in a folder · tar to bundle them into one |
🔢 FAQ & Recommended Content
Search
Linux grep command
The grep command for finding a string in text files such as logs, with examples that pipe it together with tail and find.
Disk
du command: checking file disk usage
The du command for checking how much disk space files and directories use.
Viewing files
Linux head command & tail command
The head and tail commands for viewing the first and last lines of a file, often used to check server access logs.
ℹ️ 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.