Copy file permissions, but not files

27,482

Solution 1

You should have a look at the --reference option for chmod:

chmod --reference version2/somefile version1/somefile

Apply find and xargs in a fitting manner and you should be fine, i.e. something like

 ~/version2$ find . -type f | xargs -I {} chmod --reference {} ../version1/{}

This even works recursively, and is robust against missing files in the target directory (bar the No such file ... errors, which can be ignored). Of course it won't do anything to files that only exist in the target directory.

Cheers,

Solution 2

You could use this script (it changes the permissions recursively but individually for each file/directory)

#!/bin/sh
chmod --reference $1 $2
if [ -d $1 ]
then
    if [ "x`ls $1`" != "x" ]
    then
        for f in `ls $1`
        do
            $0 $1/$f $2/$f
        done
    fi
fi

Run the script with arguments version2 version1

Share:
27,482
user788171
Author by

user788171

Updated on May 21, 2020

Comments

  • user788171
    user788171 about 4 years

    I have two copies of the same directory tree. They almost have the same files in both (one version may have a couple extra or missing files). However, most of the files are in common to both directories (have the same relative paths and everything).

    Assume these are in directories:

    version1/
    version2/
    

    The problem is that the permissions in version1/ got messed up, and I would like to copy over the permissions from version2/, but do it without replacing the files in version1/ which are newer.

    Is there an automated way to do this via bash? (It doesn't have to be bash, it could be some other method/programming language as well).

  • user788171
    user788171 about 11 years
    Just to confirm, in the command above, it is changing version 2 to match version 1 and not vice versa correct? Finally, this is robust against missing files?
  • Anders R. Bystrup
    Anders R. Bystrup about 11 years
    No and no, respectively.
  • Anders R. Bystrup
    Anders R. Bystrup about 11 years
    To elaborate, it copies the perms from current dir to version1, and I'm unsure whether xargs will continue if there's no matching file in the target directory - you should test before running on your live FS.
  • Anders R. Bystrup
    Anders R. Bystrup about 11 years
    I got curious and tested, see amended answer.
  • mavit
    mavit over 10 years
    Note, however, that this won't copy ACLs, which are used to set more fine-grained permissions that chmod.
  • MestreLion
    MestreLion over 9 years
    note that --reference is a GNU feature, not POSIX, so chmod may not have it in all systems.
  • Raphael_b
    Raphael_b over 9 years
    To make the command recursive to reach all sub-folder: chmod --reference version2/somefile version1/somefile
  • danuker
    danuker over 8 years
    I could save a Fedora installation by copying permissions from the live CD. Still works, 9 months later. Thank you!
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 6 years
    I think this is a great answer as do others, as evidenced by over 100 up-votes on Question and Answer. If you can't reopen this question at least migrate it to Unix & Linux where it would be more appreciated. I don't have sufficient rep here to nominate for reopening but hopefully you do.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix about 6 years
    I know this is five years old but I'm wondering if you recollect testing this at the time you posted it. I want to give it a try but don't want to muck anything up copying, pasting and running it as is without a little confirmation first.