ste*_*ver 117
cp通过添加-p或--preserve选项,您可以在复制时保留原始文件的时间戳:
Run Code Online (Sandbox Code Playgroud)-p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time? stamps), if possible additional attributes: context, links, xattr, all
所以只保留时间戳
cp --preserve=timestamps oldfile newfile
Run Code Online (Sandbox Code Playgroud)
或同时保留模式和所有权
cp --preserve oldfile newfile
Run Code Online (Sandbox Code Playgroud)
或者
cp -p oldfile newfile
Run Code Online (Sandbox Code Playgroud)
其他选项可用于递归复制 - 一个常见的选项是cp -a( cp --archive),它额外保留了符号链接。
小智 31
如果要保留原始时间戳,请使用
$ touch -r <original_file> <new_file>
Run Code Online (Sandbox Code Playgroud)
这会从另一个文件复制时间戳。
有关更多信息,请参阅此博客文章:假文件访问、修改和更改时间戳