This question already has an answer here:

I tried this command to compress all the css files in all subdirectories.

find . -iname "*.css*" -exec gzip -c '{}' > '{}'.gz  \;

But it only creates a {}.gz file. I ended up using this:

find . -iname "*.css" -exec sh -c "gzip -c '{}' > '{}'.gz"  \;

which works well.

The question is why the first one didn't work and second one did?

Note: I could easily use gzip -k switch to keep the source files, but gzip on CentOS 7 does not support it.

marked as duplicate by muru, pLumo, Jeff Schaller Jul 8 at 11:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Do not use {} inside sh -c. Please read this. – pLumo Jul 8 at 11:43

It's all about when and by which shell the command gets interpreted. In the first, your command line shell interprets the >, making a local file before find even starts. In the second, the subshell does, after find replaces the {}, so it works as expected.

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