Linux 复制文件权限,但不复制文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15245144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:12:15  来源:igfitidea点击:

Copy file permissions, but not files

linuxbashpermissionscopychmod

提问by user788171

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.

问题是 version1/ 中的权限搞砸了,我想从 version2/ 复制权限,但不要替换 version1/ 中较新的文件。

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

是否有通过 bash 自动执行此操作的方法?(它不一定是 bash,也可以是其他一些方法/编程语言)。

采纳答案by Anders R. Bystrup

You should have a look at the --referenceoption for chmod:

您应该查看以下--reference选项chmod

chmod --reference version2/somefile version1/somefile

Apply findand xargsin a fitting manner and you should be fine, i.e. something like

应用findxargs以装配方式,你应该罚款,即像

 ~/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.

这甚至可以递归地工作,并且对于目标目录中丢失的文件非常有效(禁止No such file ...错误,可以忽略)。当然,它不会对只存在于目标目录中的文件做任何事情。

Cheers,

干杯,

回答by swappy

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.

除非权限是细粒度的并且从一个文件到另一个文件不同,否则您可以对目录执行递归 chmod 并统一权限。

See man chmod for references on the options that might be useful

有关可能有用的选项的参考,请参阅 man chmod

回答by uba

You could use this script (it changes the permissions recursively but individually for each file/directory)

您可以使用此脚本(它递归地更改每个文件/目录的权限,但单独更改)

#!/bin/sh
chmod --reference  
if [ -d  ]
then
    if [ "x`ls `" != "x" ]
    then
        for f in `ls `
        do
            ##代码## /$f /$f
        done
    fi
fi

Run the script with arguments version2 version1

运行带有参数 version2 version1 的脚本