I have a website directory where I need to change all hardcoded links from one domain to another. Looking for a single (grep? sed?) bash command that will allow me to change all occurrences of text in all files in the directory?
Asked
Active
Viewed 2.2k times
2 Answers
48
The following will do it:
sed -i 's/old_link/new_link/g' file...
Don't forget to escape any slashes, dots, and any other regex special chars in the link addresses with a backslash.

Michael Goldshteyn
- 71,784
- 24
- 131
- 181
-
7You can use alternative delimiters in your `sed` command to avoid having to do some escaping: `sed -i 's|old/with/slashes|new/with/slashes/g'` – Dennis Williamson Nov 03 '10 at 20:18
-
I think you meant: `s|old/with/slashes|new/with/slashes|g`. Yes, you can use a character that would not be valid in a filename, but can be used by sed to delimit a regex, to avoid the escape chars. – Michael Goldshteyn Nov 03 '10 at 20:35
-
3I'm using the 'sed' that came built in to OSX, and I had to use "sed -i .backup 's/old/new/g' *" - i.e. I have to add a 'backup suffix' value for the '-i' flag, which is used to make a backup of files as sed modifies them. These are left around afterwards so I had to do a 'find' to delete them. – Jonathan Hartley Jun 07 '12 at 19:55
-
2see [this answer](http://stackoverflow.com/a/6759339/892648) for recursive replace and omiting directories – willscripted Jun 16 '13 at 00:23
-
1this answer fails if you have non-editable binary files in your directory. will-ob's link should be preferred instead. – smcg Jul 11 '13 at 14:17