Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can I copy a file's user/owner permissions to it's group permissions?

For example if the permissions are 755 I want them to become 775.

Clarification: 755 -> 775 123 -> 113 abc -> aac

Bonus if I can do this recursively for all files in a directory.

(That is, for every file the ownder permissions are copied to the group permissions. Each file may have different permissions.)

share|improve this question
    
I'm not sure to understand. You want to change the permission on directory/files for the group permission to be like the owner? If so, then just play around with chmod. I can show you a more explicit exemple in my answer if you want to. –  Anarko_Bizounours Jun 27 '11 at 10:22
    
In your answer it changes all files to be 775. What if one of the files originally has the permission 123? Then it must be changed to 113 and not 775. The permission I used in the question (755) is only an example, the actual permission may be anything. I want to do this programatically. –  Annan Jun 27 '11 at 10:26
    
the number after chmod is just numercial permission. If you do a ls -l and you get 452 as permission, then just to chmod 442 /you_directory/. Everything is explained (pretty well) in the wikipedia page I linked to my answer. –  Anarko_Bizounours Jun 27 '11 at 10:32
    
this appears to be a duplicate question of serverfault.com/questions/241082/… –  ShoeLace Sep 29 '11 at 2:24

3 Answers 3

up vote 15 down vote accepted

you can use g=u to make the group perms the same as the user perms

ls -l file
-rw------- 1 user users 0 Jun 27 13:47 file

chmod g=u file

ls -l file
-rw-rw---- 1 user users 0 Jun 27 13:47 file

and recursively

chmod -R g=u *

or for some filespec

find /patch/to/change/perms -name '*.txt' -exec chmod g=u {} +

the chmod manpage is your friend.

share|improve this answer
    
that's what I'm looking for!!! thanks :) –  Annan Jun 27 '11 at 13:04
    
good way to do it! I forgot this trick every time xD –  Anarko_Bizounours Jun 27 '11 at 13:12
    
Never heard of this trick! Usefull one. –  skinp Jun 28 '11 at 13:54

As people have said, changing file permissions can be dangerous. With great power comes great responsibility, and all that shizas. This is my answer:

file /usr/bin/otog:

#!/bin/sh

f=$1
p=`stat -c "%a" $f`
chmod ${p:0:1}${p:0:1}${p:2:1} $f

Example usage:

find . -exec otog {} \;

Uses stat to get numerical permissions, uses chmod to change permissions.

PS: Please see Iain's answer for a better way of doing this!

share|improve this answer
1  
Shouldn't it be ${p:0:1}${p:0:1}${p:2:1}? –  minaev Jun 27 '11 at 12:05
    
Yeah, your right. It weirdly worked my way as well. –  Annan Jun 27 '11 at 12:13
    
Someone please explain the downvote? –  Annan Jun 27 '11 at 13:02
    
Not me, but I'd like to compensate. This is a good solution that works. –  minaev Jun 27 '11 at 14:04
1  
I'd say it is safer to use dangerous commands in scripts than manually. In well tested scripts, that is :). On the other hand, chmod is far from being a really, really dangerous command. Compared to rm, I mean, or rsync --delete or some others. –  minaev Jun 28 '11 at 14:08

First of all, you need to list your right.

ls -l /your_directory/
-rwxr-xr-x  1   57 Jul  3 10:13  file1
-rwxr-xr-x  1   57 Jul  3 10:13  file2

to translate the result in numerical permission use this : R = 4, W = 2, X = 1.

So on this 2 file the permission are 755.

After getting your right, you must use the chmod command to change the right the way you want :

chmod 775 /your_directory/

This command will only change the right on the directory, but not on the file inside.

If you want to change the right inside too, then do this command :

chmod 775 /your_directory/ -fR

The -fR will force the recursivity. (use it only if you are sure of the right you want to apply, and the file inside the directory.)

If you only want to change the right on file1 then :

chmod 775 /your_directory/file1

And that the way the cookies crumble!

/!\ Be carefull, misuse of this command can destroy all your OS permissions and lead to malfunction of it. /!\

ps : look there to find out more information about chmod.

Hope this will help you.

share|improve this answer
    
I've added some clarification to the question. The files may not have the same owner permissions, so if chmod is to be used there must be a way of extracting the owner permissions before passing it to chmod? –  Annan Jun 27 '11 at 10:21
    
it's tricky, but the ls -l command must give you the permission, but it will be in this form -rw-rw-r--. The problem is you MUST NOT use a script to work with chmod! you must first get all the permission off the file. Then chmod to have the good right for the group. I'll edit my answer to show an exemple. –  Anarko_Bizounours Jun 27 '11 at 10:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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