我有一个包含子目录和其他文件的目录,我想用另一个文件/目录的日期/时间戳递归更新日期/时间戳。
我知道:
touch -r file directory
更改文件或目录的日期/时间戳,但不更改其中的任何内容。还有一个查找版本是:
find . -exec touch -mt 201309300223.25 {} +\;
如果我可以指定实际的文件/目录并使用另一个日期/时间戳,那会很好用。有没有简单的方法可以做到这一点?更好的是,有没有办法在执行“cp”时避免更改/更新时间戳?
最佳答案
even better, is there a way to avoid changing/updating timestamps when doing a 'cp'?
是的,使用 cp
和 -p
选项:
-p
same as --preserve=mode,ownership,timestamps
--preserve
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
例子
$ ls -ltr
-rwxrwxr-x 1 me me 368 Apr 24 10:50 old_file
$ cp old_file not_maintains <----- does not preserve time
$ cp -p old_file do_maintains <----- does preserve time
$ ls -ltr
total 28
-rwxrwxr-x 1 me me 368 Apr 24 10:50 old_file
-rwxrwxr-x 1 me me 368 Apr 24 10:50 do_maintains <----- does preserve time
-rwxrwxr-x 1 me me 368 Sep 30 11:33 not_maintains <----- does not preserve time
要根据另一个路径上的对称文件递归touch
目录上的文件,您可以尝试如下操作:
find /your/path/ -exec touch -r $(echo {} | sed "s#/your/path#/your/original/path#g") {} \;
它对我不起作用,但我想这是一个尝试/测试更多的问题。
关于bash - 用文件递归地触摸文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19090731/