bash - 用文件递归地触摸文件

标签 bash shell file touch

我有一个包含子目录和其他文件的目录,我想用另一个文件/目录的日期/时间戳递归更新日期/时间戳。

我知道:

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/

相关文章:

linux - 用于删除文件的 shell 脚本,其中文件有序列号

linux - Bash 和 awk - 在进行多行模式数学运算时如何将变量传递给 awk?

bash - 使用 grep 删除文本

c++ - 在变量中提取 C++ 图像的日期 exif

linux - 在bash中为postgresql生成随 secret 码

linux - 使用 bash 脚本将文本转换为时间格式

linux - 如何创建没有所有文件夹的 tar.gz

java - 检查文件结尾未按预期工作

c++ - 在 C++ 中打印带有扩展名的当前文件名

linux - 在 bash 中使用命令行实用程序解析变体数组