35

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

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
user788171
  • 16,753
  • 40
  • 98
  • 125
  • I created a new question in ServerFault related to this topic: [Copy file permissions, but not files in Unix](http://serverfault.com/questions/665237/copy-file-permissions-but-not-files-in-unix) – Mariano Paniga Feb 05 '15 at 12:25
  • I think this is a great question 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 OP does. – WinEunuuchs2Unix May 21 '18 at 00:05

3 Answers3

70

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,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • 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? – user788171 Mar 06 '13 at 14:44
  • 1
    No and no, respectively. – Anders R. Bystrup Mar 06 '13 at 15:01
  • 1
    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 Mar 06 '13 at 15:41
  • I got curious and tested, see amended answer. – Anders R. Bystrup Mar 06 '13 at 16:17
  • Note, however, that this won't copy ACLs, which are used to set more fine-grained permissions that chmod. – mavit Jan 15 '14 at 23:32
  • 2
    note that `--reference` is a GNU feature, not POSIX, so `chmod` may not have it in all systems. – MestreLion Jan 23 '15 at 08:28
  • To make the command recursive to reach all sub-folder: `chmod --reference version2/somefile version1/somefile` – Raphael_b Feb 13 '15 at 14:25
  • 1
    I could save a Fedora installation by copying permissions from the live CD. Still works, 9 months later. Thank you! – danuker Feb 04 '16 at 22:44
  • 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 May 21 '18 at 00:07
10

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

uba
  • 2,012
  • 18
  • 21
  • 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. – WinEunuuchs2Unix May 21 '18 at 00:20
0

You could try:

chmod owner-group-other ./dir or ./file

Unless permissions are fine grained and different from one file to another, you could do a recursive chmod on the directory and unify the permissions.

See man chmod for references on the options that might be useful

builder-7000
  • 7,131
  • 3
  • 19
  • 43
swappy
  • 108
  • 1
  • 6