Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my Linux box I have two directories:

  • work files with wrong permissions
  • older versions of the same files with the right permissions (permissions and users and groups)

I need to syncronize the permissions only without changing the file contents. I tried rsync but I can't find a suitable option. Can you give me some advice?

Thanks in advance.


Thanks to your suggestions I have this script. It recursively changes the subtree permissions:

cd good
find $1/* | while read DIR
 chown --reference="$DIR" "/bad/$DIR"
 chmod --reference="$DIR" "/bad/$DIR"

Not a masterpiece but it works for me.

share|improve this question
use a for loop over the files and then execute for each file chmod --reference=ReferenceFile and chown --reference=ReferenceFile – mailq Nov 7 '11 at 16:52
Unless there's something else to it, Maliq's suggestion should work. Just holler if you need help with the script. – Jeffery Smith Nov 7 '11 at 17:31
up vote 4 down vote accepted

You can use the --reference=file switch to both chmod and chown to do this e.g.

for FILE  in /path/to/good/directory/*
    chown --reference="$FILE" /path/to/bad/directory/"$(basename "$FILE")"
    chmod --reference="$FILE" /path/to/bad/directory/"$(basename "$FILE")"
share|improve this answer

You can try this. You need absolute paths for the 2 arguments required by this script. Run it like this source_dir target_dir

Here is the script: cat



if [ -z "$srcDir" ] || [ -z "$targDir" ]; then
    echo "Required argument missing."
elif [ ! -d "$srcDir" ] || [ ! -d "$targDir" ] ; then
    echo "Source and target not both directories."
    cd $srcDir

    echo "Source directory: $srcDir; Target directory: $targDir"
    echo "Matching permissions and ownerships .."
    find . -print0 | xargs -0I {} echo {} | xargs -I {} chmod --reference "{}" "$targDir/{}"
    find . -print0 | xargs -0I {} echo {} | xargs -I {} chown --reference "{}" "$targDir/{}"
    # find . | while read name
    # do
    #   chmod --reference "$name" "$targDir/$name"
    #   chown --reference "$name" "$targDir/$name"
    # done
    echo ".. done!"

Can adapt for more uses by using the commented out while loop but it is slower ..

$ time mperms /adp/code /adp/safe/code

Using xargs:

real 0m0.107s user 0m0.008s sys 0m0.004s

Using while loop:

real 0m0.234s user 0m0.012s sys 0m0.028s sys 0m0.028s

share|improve this answer

Your Answer


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.