Copy a file's owner permissions to group permissions

12,594

Solution 1

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.

Solution 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!

Solution 3

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:
12,594

Related videos on Youtube

AnnanFay
Author by

AnnanFay

I am an experienced developer looking to solve interesting problems and create insightful programs. My recent expertise is in using Python to investigate algorithmic efficiency, optimisation problems and visualise data.

Updated on September 18, 2022

Comments

  • AnnanFay
    AnnanFay over 1 year

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

    • Anarko_Bizounours
      Anarko_Bizounours almost 13 years
      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.
    • AnnanFay
      AnnanFay almost 13 years
      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.
    • Anarko_Bizounours
      Anarko_Bizounours almost 13 years
      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.
    • ShoeLace
      ShoeLace over 12 years
      this appears to be a duplicate question of serverfault.com/questions/241082/…
  • AnnanFay
    AnnanFay almost 13 years
    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?
  • Anarko_Bizounours
    Anarko_Bizounours almost 13 years
    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.
  • jood
    jood almost 13 years
    Shouldn't it be ${p:0:1}${p:0:1}${p:2:1}?
  • AnnanFay
    AnnanFay almost 13 years
    Yeah, your right. It weirdly worked my way as well.
  • AnnanFay
    AnnanFay almost 13 years
    Someone please explain the downvote?
  • Anarko_Bizounours
    Anarko_Bizounours almost 13 years
    good way to do it! I forgot this trick every time xD
  • jood
    jood almost 13 years
    Not me, but I'd like to compensate. This is a good solution that works.
  • Anarko_Bizounours
    Anarko_Bizounours almost 13 years
    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.
  • skinp
    skinp almost 13 years
    Never heard of this trick! Usefull one.
  • jood
    jood almost 13 years
    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.
  • Edurne Pascual
    Edurne Pascual over 7 years
    That g=u trick just saved me hours of tedious management. There goes a +1 ;-)
  • Trevor Boyd Smith
    Trevor Boyd Smith about 7 years
    i had a bunch of files owned by my user that i wanted to share with all users. so i did chmod -R ag=u ${path_to_directory}