How to find and replace all occurrences of a string recursively in a directory tree?

Using just grep and sed, how do I replace all occurrences of:


within a text file under the /home/user/ directory tree recursively finding and replacing all occurrences in all files in sub-directories as well.


Try this:

find /home/user/ -type f | xargs sed -i  's/'

In case you want to ignore dot directories

find . \( ! -regex '.*/\..*' \) -type f | xargs sed -i 's/'

Try this:

grep -rl 'SearchString' ./ | xargs sed -i 's/REPLACESTRING/WITHTHIS/g'

grep -rl will recursively search for the SEARCHSTRING in the directories ./ and will replace the strings using sed.


Replacing a name TOM with JERRY using search string as SWATKATS in directory CARTOONNETWORK

grep -rl 'SWATKATS' CARTOONNETWORK/ | xargs sed -i 's/TOM/JERRY/g'

This will replace TOM with JERRY in all the files and subdirectories under CARTOONNETWORK wherever it finds the string SWATKATS.

I know this is a really old question, but...

  1. @vehomzzz's answer uses find and xargs when the questions says explicitly grep and sed only.

  2. @EmployedRussian and @BrooksMoses tried to say it was a dup of awk and sed, but it's not - again, the question explicitly says grep and sed only.

So here is my solution, assuming you are using Bash as your shell:

for f in `grep -rl .` # Use -irl instead of -rl for case insensitive search
    sed -i 's/a\.example\.com/' $f # Use /gi instead of /g for case insensitive search

If you are using a different shell, such as Unix SHell, let me know and I will try to find a syntax adjustment.

P.S.: Here's a one-liner:

OLDIFS=$IFS;IFS=$'\n';for f in `grep -rl .`;do sed -i 's/a\.example\.com/' $f;done;IFS=$OLDIFS


For me works the next command:

find /path/to/dir -name "file.txt" | xargs sed -i 's/string_to_replace/new_string/g'

if string contains slash 'path/to/dir' it can be replace with another character to separate, like '@' instead '/'.

For example: 's@string/to/replace@new/string@g'

Try this command:

/home/user/ directory - find ./ -type f \
-exec sed -i -e 's/' {} \;

it is much simpler than that.

for i in `find *` ; do sed -i -- 's/search string/target string/g' $i;


find i => will introduce all files in the folder and in subfolders to SED.

sed -i => will replace in the files the relevant string if exists.

The command below will search all the files recursively whose name matches the search pattern and will replace the string:

find /path/to/searchdir/ -name "serachpatter" -type f | xargs sed -i 's/stringone/StrIngTwo/g'

Also if you want to limit the depth of recursion you can put the limits as well:

find /path/to/searchdir/ -name "serachpatter" -type f -maxdepth 4 -mindepth 2 | xargs sed -i 's/stringone/StrIngTwo/g'

Need Your Help

Should I use windbg_x86 for debugging 32bit applications in 64bit windows?

debugging kernel windbg x86-64 remote-debugging

I use windows 7 x64 with intel cpu. which windbg works fine and should I use for debugging x86 apps ?