我正在使用以下sed命令来查找和替换字符串:
find dir -name '*.xml' -exec sed -i -e 's/text1/text2/g' {} \;
这会更改dir中所有.xml文件的时间戳
但是,我如何保留旧的时间戳?
谢谢
解决方法:
使用stat和touch
find dir -name '*.xml' -exec bash -c 't=$(stat -c %y "$0"); sed -i -e "s/text1/text2/g" "$0"; touch -d "$t" "$0"' {} \;
使用cp和触摸
find dir -name '*.xml' -exec bash -c 'cp -p "$0" tmp; sed -i -e "s/text1/text2/g" "$0"; touch -r tmp "$0"' {} \;
来自手册:
> cp -p
-p same as –preserve=mode,ownership,timestamps
>触摸-r
-r, –reference=FILE
use this file’s times instead of current time
>触摸-d
-d, –date=STRING
parse STRING and use it instead of current time
参考:
标签:linux,sed,rhel
来源: https://codeday.me/bug/20190623/1267104.html