Copy permissions to identical tree on linux / unix

21,096

Solution 1

It can be done with the following shell line:

D1=foo; D2=foo2; for entry in $(find $D1  -exec stat -f "%N:%Mp%Lp" {} \;); do $(echo $entry | sed 's#'$D1'#'$D2'#' | awk -F: '{printf ("chmod %s %s\n", $2, $1)}') ; done

simply set the right value for D1 and D2 variables, point them to the source and destination directories, run and the dirs will have permissions in sync.

Solution 2

I just learned a new and simple way to accomplish this:

getfacl -R /path/to/source > /root/perms.acl

This will generate a list with all permissions and ownerships.

Then go to one level above the destination and restore the permissions with

setfacl --restore=/root/perms.acl

The reason you have to be one level above is that all paths in perms.acl are relative.

Should be done as root.

Solution 3

If you have the source and dest, you can synchronize your permissions with rsync -ar --perms source/ dest

It will not transfer the data, just permissions...

Solution 4

One thing you could do is use the find command to build a script with the commands you need to copy the permissions. Here is a quick example, you could do a lot more with the various printf options, including get the owner, group id, and so on.

$ find /var/log -type d -printf "chmod %m %p \n" > reset_perms
$ cat reset_perms
chmod 755 /var/log
chmod 755 /var/log/apt
chmod 750 /var/log/apache2
chmod 755 /var/log/fsck
chmod 755 /var/log/gdm
chmod 755 /var/log/cups
chmod 2750 /var/log/exim4
...
Share:
21,096

Related videos on Youtube

yawniek
Author by

yawniek

Updated on September 17, 2022

Comments

  • yawniek
    yawniek over 1 year

    i have a tree of files with correct permission. then i have a (filewise) identical tree (with different file contents tough) with wrong permissions.

    how can i transfer the permissions layout from one tree to another?

  • AWesley
    AWesley over 14 years
    It's worth mentioning that -a (for archive) is a GNU addition to cp, I've never seen it on any other system. It's just short for -dpR (no de-reference, recursive, preserve permissions). The R and p options should be in any version of cp
  • yawniek
    yawniek over 14 years
    nope, it will copy files if timestamps differ
  • Mei
    Mei over 14 years
    This assumes that stat is present. I've found, regrettably, that the command stat is often not present.
  • Mei
    Mei over 14 years
    I suspect the -printf argument to find is a GNU extension? HP-UX find doesn't have it.
  • Sandokas
    Sandokas over 14 years
    @David, I don't know of such a system lacking of stat. But it is quite trivial to use the following "octal ls" version and accommodate the given solution accordingly: alias ols="ls -la | awk '{k=0;for(i=0;i<=8;i++)k+=((substr(\$1,i+2,1)~/[rwx]/)*2^(8-‌​i));if(k)printf(\" %0o \",k);print}'"
  • mpez0
    mpez0 about 14 years
    Even without the printf option to find, one can use the ls option (or, at worst, pipe to xargs ls -l) and save in a file. A minute or two of search and replace, and one will have a script with chmod for each file.
  • Philip
    Philip about 12 years
    @yawniek The -r and --perms are redundant, but this still sync perms if they are the only thing that is different (which is what you said in the Question; if the trees are not actually identical you should not have said that they were).
  • yawniek
    yawniek about 12 years
    ok i was unclear then, i meant that the tree-structure is the same.
  • the-wabbit
    the-wabbit over 10 years
    this is a very straightforward and simple way to backup and restore permissions. Note however that getfacl and setfacl are not necessarily present on all systems.
  • sfarbota
    sfarbota over 6 years
    Is it correct to have .ac in the first command and .acl in the second?
  • TonyJ
    TonyJ over 6 years
    @sfarbota: No, it was a typo! Corrected now. Thanks for pointing it out.
  • n.st
    n.st over 6 years
    Breaks when any path involved contains any sort of special character (whitespace, starts with dashes, etc).