简体   繁体   English

如何将源文件夹中的权限复制到目标?

[英]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.

相关问题 MAC:如何合并文件夹,以便我们只在源文件比目标文件更新时覆盖? - MAC : How to merge folder so that we only overwrite when source files is newer than destination files? 如何将文件夹从Ubuntu复制到Mac - How to copy folder from Ubuntu to mac 由于 .framework 文件夹,Gulp 复制源失败 - Gulp copy source failing because of .framework folder Bash脚本将源目录复制到两个目标目录,如果成功,则验证并删除源 - Bash script to copy source catalog to two destination catalogs, verify and delete source if successful 文件夹权限 - Folder Permissions iPhone Documents Folder如何从主机移动/复制到iPhone? 令人困惑 - iPhone Documents Folder how to move/copy from host to iphone?. confusing 如何制作从源文件夹绘制的jar文件 - How to make jar files that draw from a source folder 如何将文件夹从本地Mac复制到Linux服务器 - How to copy folder from local mac to linux server 如何将文件从沙盒应用程序正确复制到应用程序脚本文件夹? - How to properly copy files to Application Scripts folder from Sandboxed App? 如何在Mac上使用Java更改文件夹权限? - How to change folder permissions with java on Mac?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM