Use sed to replace a string with one containing a directory name (i.e. a slash "/")

sed allows several syntax delimiters, / being only the one most commonly used.

You can just as well say

sed -i 's,<string>,<some/directory>,g' file.txt

where the , now has the function usually performed by the /, thereby freeing the latter from its special meaning.

Note, however (as pointed out by @Jeff Schaller), that now the , must not appear in the file or directory name - and it is a valid character for filenames! This answer gives a good overview on how to proceed when applying sed to a string with special characters.


Replacing the delimiter with one that is known not to appear in the search or replace strings is a good option when you can find a delimiter that does not appear in them. I tend to use the plus sign, rather than a comma, but that's a matter of habit more than anything.

However, if you can't find a delimiter that doesn't appear in the string, there's always backslash-escaping:

sed -e 's/<string>/<some\/directory>/g' file.txt

The \ in front of the slash lets sed know that it's just a slash and not a delimiter.