Copy file permissions, but not files [closed]

2019-03-17 13:11发布

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

3条回答
家丑人穷心不美
2楼-- · 2019-03-17 13:12

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

查看更多
We Are One
3楼-- · 2019-03-17 13:28

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

查看更多
戒情不戒烟
4楼-- · 2019-03-17 13:37

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,

查看更多
登录 后发表回答