1

Background

I was trying to gzip a folder, and all its files, recursively. I tried a command off the top of my head, and it was obviously wrong.

The result is that the folder, and all its subfolders and files, are still in place, but every file is gzipped.

E.g.

#Folder - filename.php.gz - file2.txt.gz #Subfolder -filename.php.gz etc.

I am not actually sure which command caused this. I tried a few, and most resulted in an error. But obviously one of them "worked" but didn't do what I wanted.

I suspect this is the command that caused the issue: gzip -r ocloud/ ocloud.zip

Question

What is the command I would use to reverse this? i.e. To leave all folders and files in place, but to unGzip them?

  • just note that if you had files that were gzipped before you did that, gzip would have silently skipped them. undoing the compression will then decompress all the files, even ones that were compressed to begin with. – ilkkachu Aug 7 '16 at 22:46
  • Thanks for the heads-up on that. I suspect (hope) there were no previously gzipped files in the directories in question. But this is good to know. – inspirednz Aug 7 '16 at 23:00
3

You should be able to use the -d option for that:

gzip -r -d ocloud/ ocloud.zip.gz
  • Thanks. Before I run this... I'd just like to clarify the command I suspect caused this, and your suggested remedy. There is no ocloud.zip that resulted from my command. So I assumed the ocloud.zip I had tacked on the end of there in fact did nothing. But looking at your remedy command, it suggests to me that you're expecting there to be a ocloud.zip.gz file to be sitting on my server? Yes? – inspirednz Aug 7 '16 at 22:58
  • Yes, I expect any file xxx.yy to be turned into xxx.yy.gz. Now gzip is smart enough to not gzip a file that is already gzipped. But the above command would also unzip any file that was already gzipped before you ran your gzip -r .... If there is any change for that, you might be able to distinguish the files based on their time. – Anthon Aug 8 '16 at 4:54
  • I had to run it without the ocloud.zip.gz part on the end. Got the error, ocloud.zip.gz: No such file or directory. But with that removed, this did the trick perfectly. Happened so fast (milliseconds) I wasn't sure anything had happened at all! Cheers. – inspirednz Aug 9 '16 at 0:53
  • @inspiredlife I now realise you might have put that on the original commandline as the file into which files should be zipped, instead of one of the files to zip in addition to the ones under ocloud. – Anthon Aug 9 '16 at 4:48
  • To comment on your original objective. If you want to zip a folder, you should look at zip not gzip as the latter only works on a file at a time. Or, more typical, use tar cvzf yourname.tar.gz ocloud/ to get a compressed tar archive. – Anthon Aug 9 '16 at 4:50
2

Antron has the best solution with gzip -r -d ocloud/ ocloud.zip.gz, but just because other options are nice to have, you could also do gzip -d $(find directory) when directory is the one that contains all the gziped files. Just to clarify, find recursively lists all files found in the specified directory. When you use $(someCommand), someCommand will be run, and its output will replace $(someCommand). So in effect, you would be doing gzip -d file1 subdirectory/file2 subdirectory/subsubdirectory/file3 and so on.

Also, the proper way to compress a directory with gzip is to first turn it into an uncompressed tarball with tar -cvf tarball.tar directory, and then compress the tarball with gzip tarball.tar. When decompressing, you can either repeat that backwards like gzip -d tarball.tar.gz and then tar -xvf tarball.tar, or you can let tar do everything for you with tar -xvf tarball.tar.gz.

  • Thanks John. This is very cool to know about. I always appreciate learning new tricks on Command Line, and when people take time to explain things like this. Cheers. – inspirednz Aug 9 '16 at 0:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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