gzip: Restoring original filename

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.

Identifying:

[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.

Extracting:

[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.

About Benjamin

Benjamin is a Infrastructure and Software Engineer. On this blog he writes about Linux, Docker, Programming as well as other Systems topics.

Learn more about Linux

If you liked this article, check out Benjamin's book: Red Hat Enterprise Linux Troubleshooting Guide. Where you can learn a lot more about troubleshooting Linux systems. This book is filled with tips and techniques he has learned over years of managing mission critical systems.

Related