Find with variable directory (but no -name) throws find: paths must precede expression: find

StackOverflow https://stackoverflow.com/questions/23468283

  •  15-07-2023
  •  | 
  •  

Вопрос

I'm trying to set up a shortcut function on my server for fixing permissions on new site folders (maybe this is bad practice, but I still want to solve the following problem:)

function webmod { chown -R rafe:www-data $1; find '$1' -type d -exec chmod 775 '{}' \; find '$1' -type f -exec chmod 664 '{}' \; chmod g+s -R $1; }

When I use webmod directory/name/here it just throws

find: paths must precede expression: find
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

What am I doing wrong?

Thanks in advance!

Это было полезно?

Решение

Add a semicolon between \; find. Otherwise, the first find gets the second as arguments :-)

Другие советы

Single quoted strings are not interpolated by the shell, instead of:

find '$1' ...

try:

find "$1" ...

Also, don't forget to quote the parameters to chown/chmod:

chown -R rafe:www-data "$1"

I guess best would be to have a script called in find exec, rather having complex looking find statements. If so the answer is already given by "perreal"

Or you can have a script as script1.sh

if [ -d "$1" ] ; then
    chmod 775 $1;
elif [ -f "$1" ]; then
    chmod 664 $1

fi
fi

And find will now look like:

find $1 -exec ./script1.sh {} \; chmod g+s -R $1

This way in case you want to do more on those files, you will be able to do by just extending it in your script. But for this solution to be applicable you must have luxury to have a script in your env.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top