Bash: Find and replace text strings

You don't need grep at all:

sed -i 's/ /\ /g' /path/to/test

This will escape all spaces in the file. To escape only on some strings, see Guru's answer.

Now, if you want to do that on all files which contain a space character in a given directory:

grep -rl ' ' /path/to/test/dir | xargs sed -i 's/ /\ /g'

which is, now I realize, identical to your command line, except the char after -r, which should be a lowercase L.

(Note: I'm assuming GNU tools are being used.)


One way:

sed -i 's/Alfred Hitchcock/Alfred\\ Hitchcock/' /path/to/test