36

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.)

AnnanFay
  • 600
  • 2
  • 6
  • 13
  • 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. – AnnanFay 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 http://serverfault.com/questions/241082/how-do-i-add-permissions-to-group-to-match-user-permissions/316661#316661 – ShoeLace Sep 29 '11 at 02:24

3 Answers3

72

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.

user9517
  • 114,104
  • 20
  • 206
  • 289
2

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!

AnnanFay
  • 600
  • 2
  • 6
  • 13
  • 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. – AnnanFay Jun 27 '11 at 12:13
  • Someone please explain the downvote? – AnnanFay 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
  • It's a solution that work, but not recommended. Chmod is very dangerous, and using a script to use it is more than dangerous! Chmod must always be use manually, it will keep you from error because you can see what happen and fix error! Using bash can lead to some critical error, like modifying permission on a file you shouldn't. Well, at least that's what I've been told since I started to learn bash 3 years ago. – Anarko_Bizounours Jun 28 '11 at 06:46
  • 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
1

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.

  • 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? – AnnanFay 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