0

This question is related to How to search and replace using grep Using grep and sed to find and replace a string

However, these were ambiguous as to if they searched for the string inside the files, or just their file names.

I would like to be able to search all files on the system (searching from the root directory, going into all subdirectories) for a specific string inside them (it's specifically an IP Address), and change it for another string (another IP Address), regardless of files names.

Thanks in advance

I've tried

find /path -type f -exec sed -i 's/oldstr/newstr/g' {} \;

Also

sed -i 's/\(.*\)substring\(.*\)/\1replace\2/'
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `sed` replaces inside the files. – Barmar Aug 01 '23 at 20:28
  • 1
    Your first command should do it. You can improve performance by using `+` instead of `\;` since it won't start a new `sed` for each file. – Barmar Aug 01 '23 at 20:29
  • What Barmar said ... the find **should** do the job if the `oldstr` is correct. – tink Aug 01 '23 at 20:36
  • Does this answer your question? [How can I do a recursive find/replace of a string with awk or sed?](https://stackoverflow.com/questions/1583219/how-can-i-do-a-recursive-find-replace-of-a-string-with-awk-or-sed) – tink Aug 01 '23 at 20:38
  • `grep -r -F -I -l -Z -e 'oldstr' / 2>/dev/null | xargs -0 sed -e 's/oldstr/newstr/gp'` might also work. – Grobu Aug 02 '23 at 04:42

0 Answers0