我正在以这种方式使用 sed。
find . -type f -exec sed -i "s/$3/$4/g" {} +
但它会更改所有文件的日期,即使它没有在文件中找到该字符串。 我能避免吗?
最佳答案
你可以先grep你的文件,看看有没有要替换的东西:
find . -type f -exec grep -q "$3" '{}' ';' -exec sed -i "s/$3/$4/g" '{}' ';'
否则无法避免时间戳更新,因为 sed
基本上会重新创建文件,当使用 -i
时:
--in-place[=SUFFIX]
This option specifies that files are to be edited in-place. GNU sed does this by creating a temporary file and sending output to this file rather than to the standard output.
When the end of the file is reached, the temporary file is renamed to the output file’s original name.
关于linux - sed 更改文件日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46907726/