I escaped the dot correctly and sed still rewrote a file marked DO NOT CHANGE

I escaped the dot correctly and sed still rewrote a file marked DO NOT CHANGE

●2 ●36 ●100
calendar_today • schedule1 min read

Four config files, one hostname to rename. I knew the dot in api.internal is a regex wildcard, so I escaped it. grep -rn 'api.internal' unescaped matched a fourth file containing apiXinternal — a typo somebody deliberately kept. Escaped, api\.internal matched three. Good.

Then I ran the rename, and two of those three should never have changed: a line ending in # DO NOT CHANGE, and a commented-out sample. My -not -path "*/samples/*" exclusion did nothing, because the file was called sample.conf and was not inside a samples/ directory.

That is the real lesson, and escaping was never the whole answer. The dot is the trap everyone warns about; the trap that actually got me was assuming my exclusion pattern matched what I pictured. The fix is an order, not a flag — make grep -l print the file list while everything is still read-only, and read it:

find /etc/myapp -type f -name "*.conf" -exec grep -l "api\.internal" {} +

If that list surprises you, the surprise is the bug, and you found it before anything was written. Only then pipe it to sed -i.bak, so every file keeps a rollback copy — and diff one against its .bak before trusting the run. sed -i exits 0 whether it changed four files or none.

The awk summarizing stage and the incident-triage pipeline are in the find, grep, sed and awk ordering guide.


I bundled the scripts I copy onto every new server, plus 10 bonus ones, into the Production Bash Toolkit. 30-day refund if it isn't useful.

Want more #devops & #linux deep dives?

Join 6,007+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

How AI Is Changing Web Development, and What Developers Still Need to Understand

Daniel Decker - Nov 10

sed 's/port/listen_port/g' rewrote four lines of a four-line config

BashSnippets - Oct 1

Your App Feels Smart, So Why Do Users Still Leave?

kajolshah - Feb 2

A For Loop Skipped Every File With a Space and Called the Backup a Success

BashSnippets - Jun 18

A Textile Mill's Dyehouse Ramp Curve: Get One Step Wrong and the Sheet Belongs to the Master

aiomniu - Oct 18
chevron_left
3.7k Points • 138 Badges
North America • bashsnippets.xyz
79Posts
37Comments
14Connections
Linux user who got tired of Googling the same bash commands every time I sat down at a terminal. Sta... Show more

Related Jobs

View all jobs →

Commenters (This Week)

2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!