?
Solved

Script group permission change for files and folders in Linux

Posted on 2007-10-17
16
Medium Priority
?
1,026 Views
Last Modified: 2013-12-16
Can anyone help me write a script that has the following logic:

for every file and for every folder in a directory (recursively)
change the permissions so that the group adopts the permissions of the owner

This will need to be run on a Suse Linux Enterprise Server 8 and 9.

Thanks!

REB
0
Comment
Question by:reb_elmagnifico
  • 5
  • 3
  • 3
  • +2
14 Comments
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 20092839
I really didnt understand it.
- If what you want to do is to change rhe permissions of of a directory (recursively) then the command you'd execute will be:
chmod -R g-r .
this will Recursively change permissions becasue of the -R switch.
to change group permissions you'd use the g (meaning group) then a minus or plus sign (meaning you'll be adding or removing respctively the right) and the right will be one of r,w,x meaning read, write and execute.
So the example above will remove the read  permission from the group recursively.

But then you say "the group will adopt .." if you mean the group members will use these permissions after that yes they will.

Is this what you want ?

0
 
LVL 14

Accepted Solution

by:
arrkerr1024 earned 500 total points
ID: 20094286
It sounds like you want to change the group on every file to the primary group of the user that owns the file, is that correct?

Here's a script to that, in bash:

#!/bin/bash

DIR=/directory_under_which_you_want_all_groups_changed

for ITEM in `find $DIR`; do
        USER=`ls -od $ITEM | awk '{print $3}'`
        GROUP=`id -g $USER`
        chgrp $GROUP $ITEM
done
0
 
LVL 5

Author Comment

by:reb_elmagnifico
ID: 20095826
I'm sorry I wasn't more clear. Hopefully this helps:

I want the permissions of the group to match that of the owner for each file and folder.

For example,

drwxr-xr-x   6 userA grp1   152 Oct  9 15:05 directoryA

would change to

drwxrwxr-x   6 userA grp1   152 Oct  9 15:05 directoryA

So the group permissions change to match the user's permisssions.

Does that help?

REB
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Tintin
ID: 20096917
In that case:

find /some/path -type d -perm -u+rwx -exec chmod g+rwx {} \;
find /some/path -type d -perm -u+rx -a ! -perm u+w -exec chmod g+rx {} \;
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 20097003
it is close but not perfect tintin what if thefile has w permission already set ? then you'll grant r and x.
So I think you should recursively reset all group permissions first.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20097258
The find commands I supplied will change directories with perms:

7xx to 77x
5xx to 55x

0
 
LVL 30

Assisted Solution

by:Kerem ERSOY
Kerem ERSOY earned 500 total points
ID: 20097299
Let assume that the original permission is 575 then it wil only set it to 575 again since you only check the User permision has write flag off nt the group.
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 20097305
g+rx will not reset already exiting write
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 500 total points
ID: 20097396
Ahh, I see what you mean.  In that case:

find /some/path -type d -perm -u+rwx -exec chmod g=rwx {} \;
find /some/path -type d -perm -u+rx -a ! -perm u+w -exec chmod g=rx {} \;
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 20097422
;-)
0
 
LVL 19

Assisted Solution

by:Gabriel Orozco
Gabriel Orozco earned 500 total points
ID: 20121209
mmhh...

to replicate the same set of permissions that the owner have, I wrote this:

DIR=/directory_under_which_you_want_all_groups_changed
find $DIR | while read file;do chmod g=$(ls -ld $file | cut -c 2-4 | sed 's/-//') $file;done


I tested it and it works. it will replace permissions on directories and files. if you want this only for files:

DIR=/directory_under_which_you_want_all_groups_changed
find $DIR -type f | while read file;do chmod g=$(ls -ld $file | cut -c 2-4 | sed 's/-//') $file;done
0
 
LVL 5

Author Comment

by:reb_elmagnifico
ID: 20124410
Sorry I haven't been involved in this discussion.  Thanks for all of the great replies!

Redimido, that looks like what I need.  I will get a chance to test it out in the next couple of days.  I'll let you know.

Thanks

REB
0
 
LVL 19

Expert Comment

by:Gabriel Orozco
ID: 22452609
Hi rindi

I think I understood what the used wanted to do, and it was not set the group to a fixed permissions, but if the user have r-x, then the group have r-x, if the user has r--, then the group have r--

my script is the only one that does exactly that. Not sure why REB did not award the points already.
0
 
LVL 5

Author Comment

by:reb_elmagnifico
ID: 22525406
Sorry guys for not awarding points.  I never got around to testing it.  I agree that Redimido's script looks good and deserves the points.

Thanks

REB
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

BIND is the most widely used Name Server. A Name Server is the one that translates a site name to it's IP address. There is a new bug in BIND (https://kb.isc.org/article/AA-01272), affecting all versions of BIND 9 from BIND 9.1.0 (inclusive) thro…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question