parti
parti

Reputation: 215

Gzip CSS/JS Using OS X Terminal

I am trying to use OSX Terminal to create gzip versions (.gz) of all css & js files in a folder. I found the following command, but when I cd to a test folder & then enter the command, it doesn't output anything & I would expect it to create a gzip copy in the folder next to the original file:

find . -regex ".*\(css\|js\)$" -exec bash -c 'echo Compressing "{}" && gzip -c --best "{}" > "{}.gz"' \;

What am I doing wrong? Or does the command need to be modified?

Upvotes: 1

Views: 1054

Answers (1)

baf
baf

Reputation: 4671

It seems that OS X version of find doesn't support extended regular expressions. Simple workaround would be to use logical or operator (-o option) like this:

find . \( -name "*\.js" -o -name "*\.css" \) -exec bash -c 'echo Compressing "{}" && gzip -c --best "{}" > "{}.gz"' \;

It will search for both file extensions and exec bash command for each found file.

Update. I actually found out that your syntax will also work. You need to use -E option of find command to work with extended regular expressions. You may also need to enclose the pattern in double quotes.

find -E . -regex "".*\(css\|js\)$"" -exec bash -c 'echo Compressing "{}" && gzip -c --best "{}" > "{}.gz"' \;

From find man page:

-E   Interpret regular expressions followed by -regex and -iregex         
     primaries as extended (modern) regular expressions rather than basic
     regular expressions (BRE's).  The re_format(7) manual page fully
     describes both formats.

Upvotes: 3

Related Questions