1

Because of the default umask settings on my systems, file permissions always default to no access for group and other. This is fine typically but annoying when I'm installing software that I need others to access. Is there a quick way I can reset permissions of all files and folders in a tree after an install based on the use permissions.

Basically copy user except for write.

rwx------ to rwxr-xr-x
rw------- to rw-r--r--
4
  • You need to use --recursive option.
    – direprobs
    Sep 7, 2017 at 21:04
  • 5
    Why not just change the umask setting from the default to something you like before you do the install?
    – John1024
    Sep 7, 2017 at 21:05
  • @John1024 Even better.
    – direprobs
    Sep 7, 2017 at 21:05
  • I thought of that while I was typing this question, but the damage is already done
    – eng3
    Sep 8, 2017 at 13:25

3 Answers 3

3

You can use find to do this :

find <dirpath> -perm 700 -type d -exec chmod 755 {} \;  ## For directories
find <dirpath> -perm 600 -type f -exec chmod 644 {} \;  ## For files
1
  • 2
    You could also add the "-perm 600" and "-perm 700" if you wanted to only modify the files with the permissions mentioned in the initial question; find <dirpath> -perm 700 -type d -exec chmod 755 {} \; find <dirpath> -perm 600 -type f -exec chmod 644 {} \; Sep 7, 2017 at 21:11
1

You could do something like:

find . ! -type l -print0 |
  perl -0 -lne '
    $m = (lstat$_)[2] & 07777;
    $u = ($m >> 6) & 5;
    chmod $m | $u | ($u<<3), $_'

That is, take the user permissions, remove the write bit (& 5) and or that to the group and other permissions.

0

Assuming that you're a well behaved unix user and don't have whitespaces in filenames:

for file in $(find . -type f -o -type d)
do \
    user_perm=$(stat -c %a $file |cut -c1)
    other_perm=$(($user_perm&5))
    chmod $user_perm$other_perm$other_perm $file
done
3
  • 1
    It's not only whitespace, it's any character in $IFS and wildcards Sep 7, 2017 at 22:00
  • 1
    That expects the GNU implementation of stat. Note that GNU find can output that same information by itself (-printf '%m') Sep 7, 2017 at 22:02
  • 1
    That approach won't work for files with no user permissions or files with setuid/setgid/stickybit... Sep 7, 2017 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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