Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I accidently gzip -rv directory and now all individual files is in .gz format. How do I undo this and gunzip all .gz files within the directory?

And how do I just gzip this whole directory?

I tried gunzip directory/*.gz but only gunzip the files in directory/ it doesn't gunzip directory beyond "directory".

share|improve this question
up vote 6 down vote accepted

To unzip your files, do the following:

find /yourdirectory -name *gz | xargs gunzip

This will unzip all files with the name *.gz find can find under the directory /yourdirectory.

For gzipping a whole directory, you need a tar container, as gzip can only compress individual files:

tar czvf archive.tgz /yourdirectory

If you want the (much slower but much more effective) bzip2 compression, use the j instead the z parameter:

tar cjvf archive.tbz2 /yourdirectory

Both commands will create a compressed archive, there is no need to do an additional compression step afterwards.

share|improve this answer
What command do you use exactly? – Sven Mar 13 '12 at 11:05
sorry my bad spelling mistake. it worked! – Maca Mar 13 '12 at 11:06
You just saved my arse! Thank you. – Toggo Jul 2 '14 at 21:28

Tar the directory first with tar -cf myarchive.tar myarchive/and compress it afterwards with gzip myarchive.tar.

You can unpack this afterwards with tar -xvf myarchive.tar.gz with newer versions of tar. With older versions you should add use -xvzf instead

share|improve this answer
Is there a way to undo all the .gz file? – Maca Mar 13 '12 at 10:52
Switch into the directory and type gunzip *. – Chris Mar 13 '12 at 10:56
The additional gzip step is unnecessary, and find exist. – Sven Mar 13 '12 at 10:59
Certainly you can add a pipe to run it in one line, however I fail to see why you would downrate my reply as it is totally correct and a valid reply to one of Macas questions. – Chris Mar 13 '12 at 11:05
It's for the recommendation to manually switch into the directory and do a gunzip. That's what the OP already did and wanted to avoid to do recursively. – Sven Mar 13 '12 at 11:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.