在我的Linux中,我有两个目录:
我只需要同步权限而不更改文件内容。 我试过rsync,但我找不到合适的选项。 你能给我一些build议吗?
提前致谢。
编辑
感谢您的build议,我有这个脚本。 它recursion地更改子树权限:
#!/bin/bash cd good find $1/* | while read DIR do chown --reference="$DIR" "/bad/$DIR" chmod --reference="$DIR" "/bad/$DIR" done
不是一个杰作,但它适用于我。
你可以使用--reference=file开关同时使用chmod和chown来完成这个任务
#!/bin/bash for FILE in /path/to/good/directory/* do chown --reference="$FILE" /path/to/bad/directory/"$(basename "$FILE")" chmod --reference="$FILE" /path/to/bad/directory/"$(basename "$FILE")" done
你可以试试这个 您需要此脚本所需的2个参数的绝对path。 像这样运行它copyperms.sh source_dir target_dir
这里是脚本: cat copyperms.sh
#!/bin/bash srcDir="$1" targDir="$2" if [ -z "$srcDir" ] || [ -z "$targDir" ]; then echo "Required argument missing." elif [ ! -d "$srcDir" ] || [ ! -d "$targDir" ] ; then echo "Source and target not both directories." exit else cd $srcDir echo "Source directory: $srcDir; Target directory: $targDir" echo "Matching permissions and ownerships .." find . -print0 | xargs -0I {} echo {} | xargs -I {} chmod --reference "{}" "$targDir/{}" find . -print0 | xargs -0I {} echo {} | xargs -I {} chown --reference "{}" "$targDir/{}" # find . | while read name # do # chmod --reference "$name" "$targDir/$name" # chown --reference "$name" "$targDir/$name" # done echo ".. done!" fi
通过使用注释掉的while循环可以适应更多的用途,但速度更慢。
$ time mperms / adp / code / adp / safe / code
使用xargs:
实际0m0.107s用户0m0.008s系统0m0.004s
使用while循环:
real 0m0.234s用户0m0.012s sys 0m0.028s sys 0m0.028s