1

Let's say I have a directory called permissions, that contains several files with different access rights.

What I want to do is if a file has access rights for others category, I want to accord the same access rights for the group category.

example1: if file_1 has r-x---r--, then I want that file to have the permissions r-xr--r--

example2:

file_2 has r-xr----x rights, I want it to become r-xr-x--x

example3:

file_3 and file_4 have rw---x--- and rwxr----- access respectively, these files should stay unchanged

I know with find I can search for such files with -perm 0740 then -exec chomd --- \; to change their value, but how to search only the files that have other permissions different than group permission, and when found, change their group access rights to match other rights

1
  • You can find files with other permissions by find . -perm /o=rwx. You need a script after -exec to perform the correct chmod. – berndbausch Feb 18 at 22:57
4

chmod can do this by itself:

chmod -R g+o dir

A combination of the letters ugoa controls which users' access to the file will be changed: [...] other users in the file's group (g), [...]

The operator + causes the selected file mode bits to be added to the existing file mode bits of each file [...]

The letters rwxXst select file mode bits for the affected users [...] Instead of one or more of these letters, you can specify exactly one of the letters ugo: [...] and the permissions granted to users that are in neither of the two preceding categories (o).

2

Using GNU find and a loop, you could search for each of the r w x permissions which is not set for the group, but set for others and set it for group:

for perm in r w x; do
  find . -type f \! -perm /g+"$perm" -perm /o+"$perm" -exec chmod g+"$perm" {} \;
done

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.