Replace a string with another string in all files below my current dir

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

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

Solution using find, args and sed:

find . -name '*.php' -print0 | xargs -0 sed -i 's/www.fubar.com/www.fubar.ftw.com/g'

A pure bash solution

#!/bin/bash
shopt -s nullglob
for file in *.php
do
    while read -r line
    do
       echo "${line/www.fubar.com/www.fubar.ftw.com}"
    done < "$file" > tempo && mv tempo "$file"

done