我有一个目录,其中包含子目录和其他文件,并希望更新日期/时间戳递归与日期/时间戳的另一个文件/目录。
我知道:
touch -r file directory
更改文件或目录的日期/时间戳,但不更改其中的任何内容。还有一个find版本,它是:
find . -exec touch -mt 201309300223.25 {} +\;
如果我能指定实际的文件/目录并使用另一个日期/时间戳,这将很好地工作。有没有一个简单的方法来做到这一点?更好的是,有没有一种方法可以避免在进行“CP”时更改/更新时间戳?
3条答案
按热度按时间jvidinwx1#
更好的是,有没有一种方法可以避免在进行“CP”时更改/更新时间戳?
是,使用
cp
和-p
选项:同--preserve=mode,ownership,timestamps
--保存
保留指定的属性(默认值:模式、所有权、时间戳),如果可能的话,附加属性:context,links,xattr,all
示例
要基于另一个路径上的对称文件递归地执行某个目录上的
touch
文件,可以尝试以下操作:这对我不起作用,但我想这是一个尝试/测试多一点的问题。
ldioqlga2#
正如fedorqui所说,
cp -p
是首选,但也许你忘记了-p,需要递归地将时间戳替换为原始时间戳,而无需重新复制所有文件。我尝试使用$()嵌套shell命令,但sed没有正确返回修改后的字符串。下面是一个有效的修改:这假定源的重复文件/文件夹树:
/Source
和目的地:/Destination
find
在Destination中搜索所有文件和目录(需要时间戳),-exec ... {}
为每个结果运行一个命令。bash -c ' ... '
使用bash执行shell命令。$0
保存查找结果。touch -r {timestamped_file} {file_to_stamp}
使用bash replace命令${string/search/replace}
来适当地设置时间戳源。1.源目录和目标目录被引用以处理带有空格的目录。
ui7jx7zq3#
除了'cp -p'之外,您还可以使用'touch -t'(重新)创建旧的时间戳。有关详细信息,请参阅“touch”的手册页。