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
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
.
means current directory - i.e. in this case, search in and below the current directory.sed
you need to specify an extension for the -i
option, which is used for backup files.-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.