Script group permission change for files and folders in Linux

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
LVL 5
reb_elmagnificoAsked:
Who is Participating?
 
arrkerr1024Commented:
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
 
Kerem ERSOYPresidentCommented:
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
 
reb_elmagnificoAuthor Commented:
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
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
TintinCommented:
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
 
Kerem ERSOYPresidentCommented:
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
 
TintinCommented:
The find commands I supplied will change directories with perms:

7xx to 77x
5xx to 55x

0
 
Kerem ERSOYPresidentCommented:
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
 
Kerem ERSOYPresidentCommented:
g+rx will not reset already exiting write
0
 
TintinCommented:
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
 
Kerem ERSOYPresidentCommented:
;-)
0
 
Gabriel OrozcoSolution ArchitectCommented:
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
 
reb_elmagnificoAuthor Commented:
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
 
Gabriel OrozcoSolution ArchitectCommented:
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
 
reb_elmagnificoAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.