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

●1 ●22 ●84
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.

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

My 30-day log cleanup kept a file that was 30 and a half days old

BashSnippets - Oct 8

The log was deleted. Its 48M was still allocated.

BashSnippets - Oct 28

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

BashSnippets - Oct 1

ShellCheck rates SC2086 as info. It let rm delete two files I never named.

BashSnippets - Sep 29

My Nginx Died at 2 AM and Nobody Noticed for 6 Hours. Now I Have a Watchdog Script

BashSnippets - May 21
chevron_left
3.8k Points • 107 Badges
North America • bashsnippets.xyz
68Posts
31Comments
10Connections
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)

6 comments
3 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!