[英]How to copy permissions from source folder into destination?
I have two bundles(directories) with identical hierarchy and files inside. 我有两个包含相同层次结构和文件的包(目录)。 I need to copy permissions of each item inside source bundle into files located in destinations bundle.
我需要将源包中每个项目的权限复制到位于目标包中的文件中。 I should not copy the files, only permissions.
我不应该复制文件,只能复制权限。
Is it possible to do with shell script or should I go hard way and write app to do this? 是否可以使用shell脚本或者我应该努力并编写应用程序来执行此操作?
It's not bash
, but zsh
is available in OS X, so you might try the following (I'm using % for the prompt to indicate when you are in zsh
; for>
is a secondary prompt) 它不是
bash
,但是在OS X中可以使用zsh
,因此您可以尝试以下操作(我使用%作为提示来指示您何时在zsh
; for>
是辅助提示)
$ zsh
% for x in source/**/*; do
for> chmod $(stat -f "%p" $x) ${x/^source/dest}
for> done
% exit
$
The for loop iterates over all files under source
, recursively. for循环以递归方式遍历
source
下的所有文件。 stat -f "%p" $x
outputs the permissions for a file in the source directory, to use as the argument for the command that changes the permissions of the corresponding file (ie, replace "source" with "dest" in its name) in the destination directory. stat -f "%p" $x
输出源目录中文件的权限,用作更改相应文件权限的命令的参数(即,在其名称中将“source”替换为“dest” )在目标目录中。
(Actually, that loop would work in bash
4 as well, but as you may have noticed, OS X still ships with bash
3.2.) (实际上,该循环也适用于
bash
4,但正如您可能已经注意到的那样,OS X仍然附带bash
3.2。)
As a standalone script named foo.zsh
: 作为名为
foo.zsh
的独立脚本:
#!/bin/zsh
for x in source/**/*; do
chmod $(stat -f "%p" $x) ${x/^source/dest}
done
$ chmod +x foo.zsh
$ ./foo.zsh
or simply 或者干脆
$ zsh foo.zsh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.