Search code examples
linuxcompressiongzip

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 {} \;

or

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?


Solution

  • 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" \;