Replace a string in all files under a directory and its subdirectories

Hello Friends,

I've been trying to write a script which finds a string and change it with another string. For this i want to search all files (with its arguments) under a spesific directory and its subdirectories.

For example lets assume i want to replace an IP= 192.168.0.4 with another IP=192.168.0.10 in all the files under a certain directory and its subdirectories.

Below I search for the IP that I want to change:

find . -type f -print | xargs grep 192.168.0.4

./dir1/l.txt:192.168.0.4
./dir1/subdir/m.txt:abc 192.168.04xyz

But as you know the rest is the most important part; How to replace the found IP with the other one 192.168.0.10 , for the rest i have tried to use SED and AWK gsub but was unsuccessful.

I will appreciate your helps,

Try:

find . -name "*" -type f -exec perl -i -ne 's/192.168.0.4/192.168.0.10/g;print;' {} \;

another version

find . -type f -exec sed -i 's/192.168.0.4/192.168.0.10/g' {} \;

Note: expecting your sed to support -i option.

Thanks all. Thegeek mate i believe your code should work but my environment solaris10 does not support "-i" option of SED. It didnt work. I can use the code of Dennis, but I would like to know which option should i use with SED?

With absence of -i support with sed, you would need to try something like this:

for file in $(find . -type f)
    do
           sed 's/192.168.0.4/192.168.0.10/g' $file > $file.tmp
           mv $file.tmp $file
done