jabriele
jabriele

Reputation: 11

sed changes files date

I'm using sed in this way.

find . -type f -exec sed -i "s/$3/$4/g" {} +

but it changes the dates of all files even if it does not find the string in a file. Can I avoid it?

Upvotes: 1

Views: 272

Answers (1)

zeppelin
zeppelin

Reputation: 9355

You can first grep your file, to see if there is something to replace:

find . -type f -exec grep -q "$3" '{}' ';' -exec sed -i "s/$3/$4/g" '{}' ';'

Otherwise it is not possible to avoid the timestamp update, as sed basically re-creates the file, when -i is used:

--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

Upvotes: 2

Related Questions