Stop sed from breaking when your file paths have slashes
Text Processing (Grep/Sed/Awk)
Stop sed from breaking when your file paths have slashes
🧩 The Challenge
Trying to replace a directory path in a config file using sed is a total headache because the forward slashes in your path fight with the sed delimiter. I’ve wasted way too much time debugging “bad command” errors just because my path had a subdirectory in it.
💡 The Fix
You can use almost any character as a delimiter instead of the standard forward slash. It turns a messy, escaped disaster into a one-liner that actually reads like English.
sed -i 's|/var/www/old-site|/srv/new-site|g' config.conf
⚙️ Why It Works
By picking a character that won’t appear in your input data—like a pipe or a colon—you stop the need for backslash-escaping every single directory separator. It’s a simple trick that cleans up your scripts immediately.
🚀 Pro-Tip: Use the pipe symbol as a default for all path replacements and you’ll never look back at escaping slashes again.
Linux Tips & Tricks | © ngelinux.com | 8/15/2026
