gzip: Restoring original filename

Written by Benjamin Cane on 2011-10-14 | 1 min read

I can't say this has happened to me often but recently the question came up on whether or not gzip retains the original filename.

Here are the commands necessary to not only find the original filename but uncompress the file with its original filename.


[email protected]:~/Downloads$ gzip datacenter-me.jpg  
[email protected]:~/Downloads$ gzip -l datacenter-me.jpg.gz  
    compressed    uncompressed ratio uncompressed_name  
    23386        23392  0.2% datacenter-me.jpg

As you can see from the above the filename is stored in the gzip file. For this exercise we are going to rename the file to match an inode number (this will come in handy if you have disk corruption).

[email protected]:~/Downloads$ mv datacenter-me.jpg.gz 12345  
[email protected]:~/Downloads$ gzip -l noname  
    compressed    uncompressed ratio uncompressed_name  
    23386        23392  0.2% 12345

When you rename the file and run the same command the name changes. This is because gzip will try to create a similar filename to the current filename.

[email protected]:~/Downloads$ gzip --name -l 12345  
    compressed    uncompressed ratio uncompressed_name  
    23386        23392  0.2% datacenter-me.jpg

If you add the --name flag you will now see the original filename.


[email protected]:~/Downloads$ gunzip 12345  
gzip: 12345: unknown suffix -- ignored

As you can see gunzip will not extract the file when it does not have a known Suffix such as .gz. This is because gunzip cannot determine how to name the new uncompressed file.

[email protected]:~/Downloads$ gunzip -S "" 12345  
gzip: 12345 already exists; do you wish to overwrite (y or n)? n  
not overwritten

If you add the -S flag you can specify the suffix, in our case it is a blank suffix. However gunzip tries to create the file with its new name.

[email protected]:~/Downloads$ gunzip --name -S "" 12345  
[email protected]:~/Downloads$ ls -la datacenter-me.jpg  
-rw-r--r-- 1 madflojo madflojo 23392 2011-10-06 13:47 datacenter-me.jpg

By specifying a blank suffix and the --name gunzip will uncompress the file with its original filename.

Picture of Benjamin Cane

Benjamin's specialty is keeping the lights on for mission critical systems. He is currently building applications that enable high concurrency financial transactions.

Recently Benjamin published his first book; Red Hat Enterprise Linux Troubleshooting Guide. In addition to writing, he has several Open Source projects focused on making Ops easier. These projects include Automatron, a project enabling auto-healing infrastructure for the masses.


Identify, capture and resolve common issues faced by Red Hat Enterprise Linux administrators using best practices and advanced troubleshooting techniques

What people are saying:
Excellent, excellent resource for practical guidance on how to troubleshoot a wide variety of problems on Red Hat Linux. I particularly enjoyed how the author made sure to provide solid background and practical examples. I have a lot of experience on Red Hat but still came away with some great practical tools to add to my toolkit. - Amazon Review