3

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

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

那工作得很好。除了目录和文件现在归该组所有,root并且该组也归该组所有root

如何执行此复制操作并保留 dirs & files 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]
4

5 回答 5

2

用于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
于 2014-10-01T12:14:44.380 回答
2

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

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

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

于 2014-10-01T12:17:25.047 回答
1

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

于 2014-10-01T12:14:51.247 回答
1

man cp说:

-p     same as --preserve=mode,ownership,timestamps
于 2014-10-01T12:14:54.957 回答
0

顺便说一句,避免:

cp -r src/* dest

因为它不会复制隐藏文件

在linux上做:

cp -rT src dest

在 BSD 上:

cp -R src/ dest
于 2020-08-05T10:47:51.490 回答