Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there a unix command to give the group the same permissions as the user currently has on a file-by-file basis recursively for a directory tree? I.e. if a file is user writeable it should become group writeable, otherwise it should not be group writeable and so on.

share|improve this question

migrated from stackoverflow.com Dec 15 '09 at 17:25

This question came from our site for professional and enthusiast programmers.

add comment

4 Answers 4

Kudos to jamessan for showing us g=u. On my system, this seems to work:

chmod -R g=u dir
share|improve this answer
1  
That also changes the permissions of directories. The original question specified files. –  jamessan Dec 15 '09 at 17:43
    
@jamessan, which directories are. –  Mike Graham Apr 6 '11 at 14:32
    
@Mike, just because *nix allows you to treat pretty much everything as a file, doesn't mean that directories actually are files. Even if one were to concede that they are the same, there is a distinct functional difference between changing the permissions of all files in a directory tree and changing the permissions of all files & directories in a directory tree. –  jamessan Apr 6 '11 at 17:41
    
Once again there is straightforward solution to my problem. Why am I not surprised? –  rzetterberg Jan 5 '12 at 8:05
add comment
find dir -type f -exec chmod g=u '{}' \+
share|improve this answer
add comment

I can't think of an easy way to do that with existing commands. Maybe a script like this would help :

#!/bin/bash

DIR="$1"

find "$DIR" -ls | while read a b perm c d e f g h i file; do
   uperm=${perm:1:3}
   uperm=$(echo "$uperm" | tr -d '-')
   chmod g=$uperm "$file"
done

Also, keep in mind that some perms for users might not apply to groups, and vice versa.

share|improve this answer
add comment

I don't know if such a command exists but making use of find and invoking it a few times you can achieve
what you are trying to do, for example

For example:
% find . -type f -perm -u+w -and ! -perm -g+w -exec chmod g+w {} \;

The above command traverses the current directory ".", finds all the files that have write permission for
the user but no write permissions for the group and changes there permission to be group writable.

Similarly you use variant above invocation to change the file permissions for read and execute mode for the group.

share|improve this answer
add comment

Your Answer

 
discard

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