How to copy owner permissions to group


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to copy owner permissions to group
# 1  
Old 01-01-2008
How to copy owner permissions to group

Hi,
I need a command or a script to change the group permissions to be the same as the owner permissions for all my files and directories (recursive)
any idea ?
# 2  
Old 01-01-2008
Ok, I found a sollution Smilie

Code:
#!/bin/ksh
echo "Enter Base Directory: "
read source_dir
for file in `find $source_dir`
do
#full=`ls -ld $file |awk '{print $1}'`
owner=`ls -ld $file | cut -c2-4`
#echo "$full $file ----------------> chmod g+$owner $file"
chmod g+$owner $file
done

Thanks anyway
# 3  
Old 06-25-2008
A substantially better solution, which correctly handles filenames with spaces in them:
Code:
find $1 -exec /bin/sh -c 'chmod g+`ls -ld "{}" | cut -c2-4` "{}"' \;

Either put it in a shell script, or run as-is, replacing $1 with whatever you want to process.
# 4  
Old 07-22-2008
Quote:
Originally Posted by bo0ork
Code:
find $1 -exec /bin/sh -c 'chmod g+`ls -ld "{}" | cut -c2-4` "{}"'

This does not work for all permissions. For example, for files with permissions like 500 (r-x------) your code will try to set permissions like this:

Code:
chmod g+r-x "foo"

Also, you want to use "g=" not "g+". Using "g+" will add permissions, so if group already has a permission the user does not then you won't end up with an exact copy. You will end up with group having more permissions than user. Granted it is probably unlikely that group would have a permission that user does not have. But the following would be more correct and not take any chances. I think this is closer to what you want:
Code:
find $1 -exec /bin/sh -c 'chmod g=`ls -ld "{}" | cut -c2-4 | tr -d "-"` "{}"'

Now the only other problem is that this is terribly slow because it starts up a shell for each and every file and directory in the tree. Not sure how to fix that since you to have find invoke a shell to interpret the pipeline.

Last edited by noahspurrier; 07-23-2008 at 11:05 PM..
# 5  
Old 09-04-2008
If you have Perl 5.x installed, this works:

find . | perl -ne 'chomp; $a = (stat $_)[2] & 07777; $a = ($a & 07707) | (($a >> 3) & 070); chmod($a, $_)'

Just change the "." parameter of find to whatever your base directory is. This creates only two processes and correctly handles any combination of permissions you can come up with.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Files without owner and group

Dears it is normal that the below binaries stay without any owner and group I have checked it in many servers and the like the below /usr/lpp/bos.net/inst_root/etc/ipsec# ls -lrt total 248 -r-xr-xr-x 1 987 987 13589 Jun 29 2005 default_group -r-xr-xr-x ... (5 Replies)
Discussion started by: thecobra151
5 Replies

2. Emergency UNIX and Linux Support

To identify the group owner

If I have to identify the group owner of an AIX group, what is the command to be used. Example: there is an mqadm group, how do I find the owner of this group? Please help. (6 Replies)
Discussion started by: ggayathri
6 Replies

3. UNIX for Dummies Questions & Answers

Removing permissions from all users including owner

Hello all: I will include a "requirement" for an issue I am attempting to solve for my boss. Basically, he would like to know if there is a way to prevent users and owner from editing 'write' script in Vi. - While working in Unix Vi, users would be able to keep all the previous versions... (15 Replies)
Discussion started by: bruski4
15 Replies

4. UNIX for Dummies Questions & Answers

Finding the Group Owner Name

Hi all, How can i find the group owner name...??? Thanks (4 Replies)
Discussion started by: mansahr143
4 Replies

5. UNIX for Dummies Questions & Answers

Setting group and owner with mkdir?

Hi, As root, I want to create a directory and set the group and ownership permissions at the same time with one command, instead of making the directory, then going back and doing a chown and chgrp. I don't see an option for this in the mkdir man page. Would I pipe chown and chgrp with my... (1 Reply)
Discussion started by: moose123
1 Replies

6. UNIX for Dummies Questions & Answers

Using find to search for any owner having execute permissions.

Hi I need help. I need to use find (or grep I don't care) to recursively search for files who have any kind of executable permissions (group and/or owner and/or other). I am looking for *.c and *.h This what I am using now: find . -name *.h -perm -111 -print but I don't want to retype that... (4 Replies)
Discussion started by: dissectcode
4 Replies

7. Shell Programming and Scripting

automatically change owner and group

We have a program that when a new account is created using the webpage it creates a new directory on the linux filesystem for the account. The problem is the process that creates the directory is as root user, as I want ftpuser to be able to login I have to manually login and chown -R the... (1 Reply)
Discussion started by: borderblaster
1 Replies

8. Shell Programming and Scripting

How can i copy user permissions(privileges) to a group

Hey there I have a problem and i was hoping that you guys could help me out I want to copy a user privileges to a group and i need to copy all privileges(Recursively) every directory with all its sub directories and I tried some solution and it did not work. I used the following command:- ... (14 Replies)
Discussion started by: The Dark Knight
14 Replies

9. Shell Programming and Scripting

permission, owner and group

hello I search a script (ksh for Aix 5.3) to save all permissions, groups and owner for all files. Because we work much to change it, and a mystake ......! So i want execute this script to save/ execute permissions for all files. If you have this script, thank you for your help ;) best... (2 Replies)
Discussion started by: pascalbout
2 Replies

10. UNIX for Dummies Questions & Answers

owner and group in Linux

I am bit unclear of how Linux was set in the real world, please advise me how it's supposed to be. When I log in as root and do a ls -l, I find: /boot, /, /var, /usr, /tmp, /home, /u01, /u02, /u03 and of of this partition is owned by root and the group also belong to root. Is that the way it's... (1 Reply)
Discussion started by: lapnguyen
1 Replies
Login or Register to Ask a Question