【问题标题】:UNIX How to copy entire directory (subdirectories and files) from one location to another and retain permissionsUNIX 如何将整个目录(子目录和文件)从一个位置复制到另一个位置并保留权限
【发布时间】:2014-11-26 06:39:37
【问题描述】:

我刚刚将整个目录结构从我服务器上的一个位置复制到另一个位置:

cp -r /home/abc/public_html/* /home/xyz/public_html/

效果很好。除了目录和文件现在由 root 拥有,并且组也是 root

如何执行此复制操作并保留目录和文件 ownershipgroupspermission 设置?

这是我的man cp

NAME
       cp - copy files and directories

SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
       Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

       Mandatory arguments to long options are mandatory for short options too.

       -a, --archive
              same as -dR --preserve=all

       --backup[=CONTROL]
              make a backup of each existing destination file

       -b     like --backup but does not accept an argument

       --copy-contents
              copy contents of special files when recursive

       -d     same as --no-dereference --preserve=links

       -f, --force
              if an existing destination file cannot be opened, remove it and try again (redundant if the -n option is used)

       -i, --interactive
              prompt before overwrite (overrides a previous -n option)

       -H     follow command-line symbolic links in SOURCE

       -l, --link
              link files instead of copying

       -L, --dereference
              always follow symbolic links in SOURCE

       -n, --no-clobber
              do not overwrite an existing file (overrides a previous -i option)

       -P, --no-dereference
              never follow symbolic links in SOURCE

       -p     same as --preserve=mode,ownership,timestamps

       --preserve[=ATTR_LIST]

【问题讨论】:

    标签: chmod cp chown chgrp


    【解决方案1】:

    使用cp -a 复制权限和用户/组。

    手册页对此进行了解释:

    -a, --archive
        same as -dR --preserve=all
    ...
    -d     same as --no-dereference --preserve=links
    ...
    -R, -r, --recursive
       copy directories recursively
    ...
    --preserve[=ATTR_LIST]
       preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes:
       context, links, xattr, all
    

    【讨论】:

      【解决方案2】:

      来自man cp

      -p    Cause cp to preserve the following attributes of each source file
             in the copy: modification time, access time, file flags, file mode,
             ACL, user ID, and group ID, as allowed by permissions.
      

      所以 cp -pr

      【讨论】:

      • 所以-p-a @Doon 更好?
      • 定义更好? -a == -RpP ,因此您使用 -a 的情况将是 cp -a /home/abc/public_html/ /home/xyz/public_html/ ,因为您将 -a 指向目录,而不是文件列表。但要注意 -a 符号链接不会被遵循,所以这可能是也可能不是你想要的。
      • "better" -- 我想保留(或保留)所有者、组、权限。 “更好”意味着哪一个会为我做到这一点。
      • 为了进一步混淆您需要检查您的 cp 版本(因为 BSD 版本似乎使用略有不同的选项)请参阅@tlund 的答案)
      • 两者的作用基本相同,具体取决于您的操作系统/cp 版本
      【解决方案3】:

      man cp 说:

      -p     same as --preserve=mode,ownership,timestamps
      

      【讨论】:

        【解决方案4】:

        对于某些与 unix 相关的操作系统(例如带有 coreutils 中的“cp”的 Linux),答案“cp -a”是正确的,但不是全部。

        “cp -rp”对其他一些是正确的。

        您应该查阅特定操作系统上的“cp”手册页。或者让我们知道您使用的是什么。

        【讨论】:

          【解决方案5】:

          顺便说一句,避免:

          cp -r src/* dest
          

          因为它不会复制隐藏文件

          在 linux 上做:

          cp -rT src dest
          

          在 BSD 上:

          cp -R src/ dest
          

          【讨论】:

            猜你喜欢
            • 2013-07-12
            • 2012-08-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-04
            • 1970-01-01
            • 2014-02-04
            • 2013-05-02
            相关资源
            最近更新 更多