bash 用文件递归触摸文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19090731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recursively touch files with file
提问by Cocoa Puffs
I have a directory that contains sub-directories and other files and would like to update the date/timestamps recursively with the date/timestamp of another file/directory.
我有一个包含子目录和其他文件的目录,并且想用另一个文件/目录的日期/时间戳递归更新日期/时间戳。
I'm aware that:
我知道:
touch -r file directory
changes the date/timestamp for the file or directory with the others, but nothing within it. There's also the find version which is:
使用其他文件或目录更改文件或目录的日期/时间戳,但不更改其中的任何内容。还有 find 版本,它是:
find . -exec touch -mt 201309300223.25 {} +\;
which would work fine if i could specify the actual file/directory and use anothers date/timestamp. Is there a simple way to do this? even better, is there a way to avoid changing/updating timestamps when doing a 'cp'?
如果我可以指定实际的文件/目录并使用另一个日期/时间戳,这将工作正常。有没有一种简单的方法可以做到这一点?更好的是,有没有办法在执行“cp”时避免更改/更新时间戳?
采纳答案by fedorqui 'SO stop harming'
even better, is there a way to avoid changing/updating timestamps when doing a 'cp'?
更好的是,有没有办法在执行“cp”时避免更改/更新时间戳?
Yes, use cp
with the -p
option:
是的,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
-p
与 --preserve=mode,ownership,timestamps 相同
- 保存
保留指定的属性(默认:模式、所有权、时间戳),如果可能的话,附加属性:上下文、链接、xattr、所有
Example
例子
$ 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
To recursively touch
files on a directory based on the symmetric file on another path, you can try something like the following:
要touch
基于另一个路径上的对称文件递归地在目录上创建文件,您可以尝试以下操作:
find /your/path/ -exec touch -r $(echo {} | sed "s#/your/path#/your/original/path#g") {} \;
It is not working for me, but I guess it is a matter of try/test a little bit more.
它对我不起作用,但我想这是更多尝试/测试的问题。
回答by philshem
In addition to 'cp -p', you can (re)create an old timestamp using 'touch -t'. See the man page of 'touch' for more details.
除了“cp -p”之外,您还可以使用“touch -t”(重新)创建旧时间戳。有关更多详细信息,请参阅“触摸”的手册页。
touch -t 200510071138 old_file.dat