Charles L. Charles L. - 10 months ago 59
Linux Question

Gzip multiple files individually and keep the original files

I am looking to gzip multiple files (into multiple .gz files) in a directory while keeping the originals.

I can do individual files using these commands:

find . -type f -name "*cache.html" -exec gzip {} \;


gzip *cache.html

but neither preserves the original. I tried

find . -type f -name "*cache.html" -exec gzip -c {} > {}.gz

but that only made a {}.gz file. Is there a simple way to do this?

Answer Source

Your > in the last command gets parsed by the same shell which runs find. Use a nested shell:

find . -type f -name "*cache.html" -exec sh -c "gzip < {} > {}.gz" \;