Copy permissions to identical tree on linux / unix
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
...