I need Gzip to pre-zip some static files for a webserver I'm building. All of the files I need to gzip will be in a folder named .cache. Since some clients may not accept responses that are gzipped, I would like to keep all of the original files when I gzip them. Is there any command I can use to recursively gzip the files in my .cache folder while still keeping the original files? Will this command gzip gzipped files (ones that are postfixed with .gz already) if run on a folder with already gzipped files?

While we're on the topic of gzip: I've been looking for a way to gzip text input passed to gzip instead of files. I came up with postfixing a dash on the command (like gzip -c -), but I'm not sure if that will work or how I use it. Can anyone explain?


Use the option -c to output the result to stdout. gziping all files in .cache:

for i in .cache/*; do gzip -c "$i" > "$i.gz"; done

To gzip them again and not gzip the gziped files check the suffix:

for i in .cache/*; do [ "${i:(-3)}" == ".gz" ] || gzip -c "$i" > "$i.gz"; done

So only files that not end in .gz will be gziped.

  • Thanks! What about passing text to gzip? Can gzip only work its magic on files or can you pass it a string? Jun 21 '11 at 10:18
  • What do you mean with text? Of course you can also do the following: echo word | gzip -c > test.gz. test.gz will contain the gziped word.
    – binfalse
    Jun 21 '11 at 10:24
  • I'll just do it that way... Jun 21 '11 at 11:25

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.