1

I have a need to make some folders/files (recursive) have the same permissions as that of the user. So:

  • 0755 -> 0777
  • 0644 -> 0666

How can I do so? Perhaps a little secret command I do know of?

If there is no easy way then perhaps I would need to create a loop and test/assign. Ultimately I only need to deal with 3 types:

Folders should be 777, most files 666, executables 777.

0

2 Answers 2

2

find can do the work of finding files with a given permission set and running the smallest possible number of chmod invocations necessary to cover them.

find . \
  '(' -perm -0700 -exec chmod 0777 '{}' + ')' -o \
  '(' -perm -0600 -exec chmod 0666 '{}' + ')'
-1

There is no command that does for you. You do need a little loop:

for file in $(find . -print)
do
  if [ -d $file -o -x $file ]; then
    chmod 777 $file
  else
    chmod 666 $file
  fi
done
4
  • 3
    This is very, very buggy. Look at what happens with filenames with whitespace or wildcards. Also, run it through shellcheck.net and fix the quoting issues. Dec 20, 2016 at 18:23
  • ...and as for the general practice of for file in $(find . -print), see BashPitfalls #1, and DontReadLinesWithFor. Dec 20, 2016 at 18:25
  • ...if you want to be able to safely and correctly handle arbitrary filenames from find in your shell scripts, you either need to use an -exec action to pass them to your script on argv, or -print0 to generate a NUL-delimited stream. Search for -print0 in BashFAQ #1 for an example of the latter, or the "Complex Actions" section of UsingFind for discussion of both. Dec 20, 2016 at 18:27
  • Also, -o in [ ] is deprecated. See the POSIX standard for test, and note the [OB] ("obsolescent") notation by the specification of -a and -o. Dec 20, 2016 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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