[英]How to copy files and give them permission of destination directory
I am copying files from source to location. 我正在将文件从源复制到位置。 The source is not owned by me and the permission for files at source is ----rwx---.
源不属于我,源文件的权限是---- rwx ---。 The permission of files coped to destination directory which is owned by me is ----rx---.
处理我所拥有的目标目录的文件的权限是---- rx ---。 The permission of destination directory is drwxrwsrwx.
目标目录的权限是drwxrwsrwx。 How do I have the files with same permission of destination directory.
如何使用目标目录具有相同权限的文件。 I tried "cp --no-preserve=all" but it did not work (still the same permission).
我试过“cp --no-preserve = all”但它没有用(仍然是相同的权限)。
试试这个:
cp --no-preserve=mode,ownership $backupfile $destination
Let me rephrase that to "How to preserve permissions of destination directory on copy?" 让我重新说一下“如何在副本上保留目标目录的权限?”
I can't take credit for the answer since I just combined a couple of answers I found on the wild. 我不能赞同答案,因为我只是结合了我在野外发现的几个答案。 So here it comes.
所以它来了。
Permissions are generally not propagated by the directory that files are being copied into, rather new permissions are controlled by the user's umask .
权限通常不会被复制到文件的目录传播,而新权限则由用户的umask控制 。 However when you copy a file from one location to another it's a bit of a special case where the user's umask is essentially ignored and the existing permissions on the file are preserved.
但是,当您将文件从一个位置复制到另一个位置时,这是一种特殊情况,其中用户的umask基本上被忽略,并且保留了文件的现有权限。
Which explains why you can't directly propagate the permissions of the src to the dst directory. 这解释了为什么你不能直接将src的权限传播到dst目录。
However, there is two-step workaround to this. 但是,这有两步的解决方法。
#!/bin/bash
# Filename: cp-metadata
myecho=echo
src_path="$1"
dst_path="$2"
find "$src_path" |
while read src_file; do
dst_file="$dst_path${src_file#$src_path}"
$myecho chmod --reference="$src_file" "$dst_file"
$myecho chown --reference="$src_file" "$dst_file"
$myecho touch --reference="$src_file" "$dst_file"
done
You can leave out the touch
command if you don't want keep the timestamp. 如果您不想保留时间戳,可以省略
touch
命令。 Replace myecho=echo
with myecho=
to actually perform the commands. 替换
myecho=echo
与myecho=
实际执行的命令。
Mind that this script should be run in sudo
mode in order to be able to run chown
and chmod
effectively 请注意,此脚本应以
sudo
模式运行,以便能够有效地运行chown
和chmod
cp --preserve
: After you have successfully run the first command now it's time to copy the contents along with the attributes to the dst directory. cp --preserve
:现在成功运行第一个命令后,就可以将内容和属性一起复制到dst目录了。
--preserve[=ATTR_LIST]
--preserve [= ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all保留指定的属性(默认:模式,所有权,时间戳),如果可能的话还有其他属性:context,links,xattr,all
\\cp -rfp $src_dir $dst_dir
should do what you want. \\cp -rfp $src_dir $dst_dir
应该做你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.