简体   繁体   English

如何复制文件并授予它们目标目录的权限

[英]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. 所以它来了。

Firstly 首先

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. 但是,这有两步的解决方法。

  1. cp-metadata : Copy the attributes and only the attributes you want to preserve back to the source directory. cp-metadata :将属性和仅要保留的属性复制回源目录。 Here is a quick script that can do 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=echomyecho=实际执行的命令。
Mind that this script should be run in sudo mode in order to be able to run chown and chmod effectively 请注意,此脚本应以sudo模式运行,以便能够有效地运行chownchmod

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

相关问题 如何按文件类型递归查找文件并将它们复制到目录? - How to find files recursively by file type and copy them to a directory? 如何在linux中一次复制多个文件? 这些文件的源位置和目标位置是同一目录 - How do I copy multiple files at once in linux? With the source and the destination locations of these files being the same directory 如何在单个命令中创建目录并授予权限 - How to create a directory and give permission in single command 如何授予对所有文件的Apache的写权限 - How to give write permission to apache on all files 如何仅将最新的子目录复制到目标Linux Shell脚本 - How to copy only the newest sub directory to a destination linux shell script 如何在列表中查找包含使用部分名称的目录和子目录中的所有文件,然后将它们复制到新文件夹 - How to find all files in a directory and subdirectories that contain using partial names within a list then copy them to a new folder 如何授予apache从目录检索img的权限? - How do I give permission for apache to retrieve imgs from directory? 如何授予apache使用NTFS分区上的目录的权限? - How do I give apache permission to use a directory on an NTFS partition? 如何授予 apache (lamp) 在 opensuse tumbleweed 中创建目录或文件的权限? - How to give apache (lamp) permission to make directory or a file in opensuse tumbleweed? 如何为我的Laravel应用程序授予权限在目录中创建子文件夹(Ubuntu) - How to give permission for my Laravel application create subfolders in a directory (Ubuntu)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM