linux - sed 更改文件日期

标签 linux sed

我正在以这种方式使用 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.

GNU sed Manual

关于linux - sed 更改文件日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46907726/

相关文章:

bash - awk 或 Sed : Replace text between special characters (delimiters)

python - 在 linux 上用 python 从 postfix 读取邮件

linux - 如何在 shell 脚本中的 SCP 命令中附加变量?

linux - 自动启动服务失败

linux - 如何使用 Sed 仅注释掉未注释的行并保持空白/空行 100% 完好无损?

awk - 在 text.file 中切换列

regex - sed regex可以模拟后向和前瞻吗?

bash - 使用 sed 过滤列表中的学生

linux - 在命令行 (Ubuntu) 上运行 Ipython notebook 时出错

linux - 使用 awr Linux Red Hat 从文件中列出字符串