08 January 2026

linux sed sysadmin command-line

sed Search and Replace with Forward Slashes

Quick one-liner: Replace text across multiple files with sed. And when your replacement contains forward slashes, here's the trick — change the delimiter.


🤔 Why This Matters

I use sed all the time when I need to edit the same thing across multiple files. The classic syntax:

sed -i 's/search/replacement/' *.txt

This replaces every occurrence of "search" with "replacement" in all .txt files, in-place.

But what if your replacement contains a forward slash?

sed -i 's/search/replace/ment/' *.txt

That breaks — sed thinks the / after replace is the closing delimiter. You get an error like unknown option to 's'.


🔧 The Fix: Change the Delimiter

sed doesn't require / as the delimiter. You can use any character — |, #, @, : — whatever works.

sed -i "s|search|replace/ment|" *.txt

Using | as the delimiter means sed treats the forward slash in replace/ment as a regular character, not a delimiter.


✅ Real-World Examples

Replace a URL in config files:

sed -i "s|http://old.example.com|https://new.example.com:8443|" *.conf

Fix a path in multiple scripts:

sed -i "s|/opt/old/path|/opt/new/path/with/subdir|" *.sh

Update a file path in a Dockerfile:

sed -i "s|COPY ./src|COPY ./app/src|" Dockerfile

💡 Pro Tip: Test First

I always omit the -i flag first to preview the changes on screen before applying them:

# Dry run — see changes on screen
sed "s|search|replace/ment|" *.txt

# Apply changes — edit files in-place
sed -i "s|search|replace/ment|" *.txt

This way you catch mistakes before they're written to your files.


✅ Quick Reference

Command What It Does
sed 's/old/new/' file Replace first occurrence per line
sed 's/old/new/g' file Replace all occurrences (global)
sed -i 's/old/new/g' *.txt Edit files in-place
sed -i "s\|old\|new/with/slash\|g" *.txt Use | delimiter for paths
sed -i 's#/old/path#/new/path#g' *.conf Use # as delimiter