Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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 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?

share|improve this question
up vote 6 down vote accepted

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.

share|improve this answer
Thanks! What about passing text to gzip? Can gzip only work its magic on files or can you pass it a string? – Bailey Parker 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... – Bailey Parker Jun 21 '11 at 11:25

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.