Unix“cp -p”。我可以有选择地保留属性吗?

Ada*_*dam 4 permissions cp macos

我想复制修改和访问时间,而不是用户 ID。如果我使用

cp -p source target
Run Code Online (Sandbox Code Playgroud)

它会复制一切。

我正在尝试将文件复制到其他用户,但保持原始日期不变。

mpy*_*mpy 6

cp手册GNU coreutils

-p 与...一样 --preserve=mode,ownership,timestamps

所以,你正在寻找

cp --preserve=mode,timestamps source target
Run Code Online (Sandbox Code Playgroud)

但是如果您使用一些非 GNU 操作系统,您可能无法将这些长选项与cp. 在这种情况下,您可以rsync尝试一下,您可以在其中详细指定应保留哪些属性(在手册页中搜索“保留”):

    -H, --hard-links            preserve hard links
    -p, --perms                 preserve permissions
    -E, --executability         preserve executability
    -A, --acls                  preserve ACLs (implies -p)
    -X, --xattrs                preserve extended attributes
    -o, --owner                 preserve owner (super-user only)
    -g, --group                 preserve group
        --devices               preserve device files (super-user only)
        --specials              preserve special files
    -t, --times                 preserve modification times
Run Code Online (Sandbox Code Playgroud)

因此,要类似于cp上面的命令,请使用类似

rsync -pEt source target
Run Code Online (Sandbox Code Playgroud)

要预先测试命令,您可以使用-n. 还添加详细参数-v以查看发生了什么:

rsync -nv -pEt source target
Run Code Online (Sandbox Code Playgroud)

但是,我不确定访问时间是否也会被复制。

  • 然后,我认为,你的 `cp` 没有提供很长的选项。我会用 `rsync` 添加另一种可能性,也许这对你来说是一个选择。 (2认同)