Grep and Sed – How to Recursively Replace String in Files

grepsed

I want to replace the backslash in the string page_path\example_filename.txt with a forward slash. I also want to be able to run this on a large file system and have it recursively search all directories.

I found someone on the web who said to use grep, xargs, and sed but I wasn't able to get it to work. I've been trying different variations on delimiters and escape characters, but didn't get it.

Here is the command I was trying to run:
grep -lr -e 'page_path\\' * | xargs sed -i 's/page_path\/page_path//g'

Thanks in advance.

Best Answer

Also you can use find for that:

find /your/path -type f -exec grep -l 'page_path\\' {} \; -exec sed -i 's#page_path\\#page_path/#g' {} \;

The second exec will be executed only if the first one was succeed, and you won't get problems with unprintable or escape needing characters in file names.

Related Question