Issue
How do I replace every occurrence of a string with another string below my current directory?
Example: I want to replace every occurrence of www.fubar.com
with www.fubar.ftw.com
in every file under my current directory.
From research so far I have come up with
sed -i 's/www.fubar.com/www.fubar.ftw.com/g' *.php
Solution
You're on the right track, use find
to locate the files, then sed
to edit them, for example:
find . -name '*.php' -exec sed -i -e 's/www.fubar.com/www.fubar.ftw.com/g' {} \;
Notes
- The
.
means current directory - i.e. in this case, search in and below the current directory. - For some versions of
sed
you need to specify an extension for the-i
option, which is used for backup files. - The
-exec
option is followed by the command to be applied to the files found, and is terminated by a semicolon, which must be escaped, otherwise the shell consumes it before it is passed to find.
Answered By - martin clayton Answer Checked By - Mildred Charles (WPSolving Admin)