sed finds nothing but it changes file's timestamp

I must go through some files to change a certain string within text files to another string. I use openSUSE and folders are mounted by cifs.

Text to be replaced (only in .m extension) is U:\FOLDER and new string is N:

That works fine with spaces in directory names etc., but this process changes every .m file's timestamp to current. We could use those previous timestamps. That search string is included in about 5 % of those .files, so it usually doesn't change the file at all. I think I could search that file before using sed that way, but it would be slower and I have to search a lot of files.

O=$IFS
IFS=$(echo -en "\n\b")
for f in `find . | grep "\.m\>"`; 
do sed -i 's/U:\\FOLDER/N:/gi' "$f";
done

try using the cp -p (preserve's original timestamp, date and such)

What timestamp are you talking about? File modification time will remain the same for those files not affected by the substitution. Only the access time will change for these. You need to preserve these times too???

I need to preserve old modification time, at least in those cases that nothing is changed. Both Windows and openSuse shows new modification timestamps for all .m files.

I guess it's that sed parameter '-i' (in-place edit) that makes this happen? But it's so cute parameter in this case :slight_smile:

Apparently no one has solution to this problem?

You can add:

grep -q 'U:\\FOLDER' "$f" || continue;

before sed.

1 Like