Aley
Aley

Reputation: 8640

Bash: How to save 'find' options into a variable

I need to chown and chmod some directories and files. To not always duplicate the find filters, I need to save them into a variable.

The following code produces Syntax error: "(" unexpected

findOpts=( \( -path ./.git -o -name .gitignore \) -prune -o )

find . "${findOpts[@]}" chown www-data:www-data {} \+

find . "${findOpts[@]}" -type d -exec chmod 550 {} \+
find . "${findOpts[@]}" -type f -exec chmod 440 {} \+

Upvotes: 0

Views: 179

Answers (1)

Carlos Campderrós
Carlos Campderrós

Reputation: 22972

Since your chmod calls are just enabling the x bit on directories and disabling it in files, you can use X (capital x) in chmod (after clearing them first):

chmod a=,ug+rX file[..]

Also, you can use multiple -exec in find (at least in GNU find), so you could execute find only once, without need to save the options:

find . [your filters here] \
    -exec chown www-data:www-data {} \;  \
    -exec chmod a=,ug+rX {} \;

Upvotes: 2

Related Questions