📑 Daftar Isi
- Why Bulk Editing Server Config Is So Scary
- Step 1: Backup First, Don't Play Hero
- Step 2: The sed Basics You Need to Memorize
- Step 3: Bulk Editing Across Many Files at Once
- Combining sed with glob (*.conf)
- Bulk editing with find + sed (recursive)
- Bulk editing across multiple servers at once (parallel SSH)
- Step 4: Patterns I Use Every Day in Production
- Replacing words with context
- Replacing strings with inconsistent whitespace
- Commenting and uncommenting specific lines
- Step 5: Delete, Insert, and Other Operations
- Step 6: Safe In-Place Editing and Testing
- Step 7: Troubleshooting & Command Table
- FAQ
So here’s the deal — I’ve seen way too many admins open a config file, fire off a sed command on a whim, and watch an entire production service collapse because of one bad string. Seriously. This isn’t a small deal, and I’m tired of watching it happen over and over.
Back when I was a junior NOC engineer, I got tasked with changing the SSH port across 40 VPS servers at once. Picture that — 40 sshd_config files, 40 restarts, and if even one of them had a typo, I’d have 40 angry clients calling in. I got lucky, because I used sed, and ten minutes later the whole thing was done. But the nervousness was real, because we were messing with production config.
Why Bulk Editing Server Config Is So Scary
Let’s be honest about the problem before I hand you the tricks. It’s not just that there are a lot of config files — it’s that a single wrong line can turn into a disaster. One bad line in nginx.conf can drop hundreds of sites into error 502, or a typo in sshd_config can lock you out of your own remote server. The common causes are usually: human error during manual editing, the same config scattered across many servers with no sync, and only one person knowing which files actually need changing.
Now picture this: you’ve got 30 .conf files that are nearly identical, differing in just a handful of parameters. Doing that by hand? That’s easily two hours. And two hours means downtime, means angry clients, means you getting chewed out. Here’s the good news though — sed (the stream editor) makes this surprisingly easy. You just need to think about pattern matching for a moment, and then everything else pretty much takes care of itself. Take a look at the core concept before we jump into the steps.
Sed isn’t some magic app that makes errors disappear — but it’s a highly precise tool once you understand it. Unlike vi or nano where you open a file and edit one line at a time, sed processes text directly from the command line without you ever opening the file. It reads lines, matches a pattern, transforms them, and writes the result straight back — or copies it to a new file. That’s the key behind every bulk-edit trick I’m about to share below.
And one thing people often misunderstand: sed isn’t only for replacing strings. It can delete lines, insert new ones, print specific lines, and prepend or append text to every line. In other words, it’s a versatile text-manipulation tool — and when you apply it across many files at once, it becomes a NOC engineer’s best friend when you’re racing against the clock. So let’s get straight into the most important part: backing up first. Don’t skip this one — seriously, pay attention here.
Step 1: Backup First, Don’t Play Hero
This is non-negotiable. Before sed touches a single production file, back it up. I don’t care how confident you are in your command — one bad line is all it takes. Here’s how to back up:
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak.20260901
If you’re editing multiple files in one folder, back them all up with a quick loop:
mkdir -p /root/config-backup-20260901
for f in /etc/nginx/sites-available/*.conf; do
cp "$f" /root/config-backup-20260901/
done
Now here’s a smarter trick for those who love dealing with multiple files — let sed create an automatic backup of each original file using the -i flag with a suffix. Sed will save the old version as file.bak before writing the new version:
sed -i.bak 's/worker_processes 4;/worker_processes 8;/' /etc/nginx/nginx.conf
This produces two files: nginx.conf (the edited one) and nginx.conf.bak (the original). If anything goes wrong, you can restore it in a second. Trust me — this backup is the difference between a calm NOC and a panicked one at 3 AM.
Before you back up, also make sure you know what version of the config you’re currently running. Servers sometimes differ across environments, so don’t assume all files are identical. Double-check with grep or diff between servers. Makes sense, right? Because the config that’s trimmed on one server isn’t necessarily the source of the same problem on another.
Step 2: The sed Basics You Need to Memorize
Alright, this is the meat of it. The basic sed syntax looks like this:
sed [options] 's/old_pattern/new_pattern/flags' filename
The s stands for substitute. The old and new patterns are separated by the slash /. And there are flags at the end. The most common ones:
- g — replace every occurrence on each line (global); without it, sed only replaces the first one on each line
- i — case-insensitive, so it doesn’t care about uppercase or lowercase
- no flag — only replace the first occurrence on each line
Simplest example. Say you want to change every port 80 to 8080 in a vhost config file:
sed 's/80/8080/g' /etc/nginx/sites-available/example.conf
Notice: the command above only prints the result to the terminal; it doesn’t write back to the file. That’s a dry run — great for verifying your pattern is correct before you use -i. This is the safest way to test.
Once you’re confident, then write it to the original file:
sed -i 's/80/8080/g' /etc/nginx/sites-available/example.conf
See the -i there? That’s what makes sed write the result directly to the file. Without it, sed just prints to the screen and the original file stays untouched. So many beginners forget this, then get confused why the file hasn’t changed. No need to overthink it: no -i means no permanent change.
Step 3: Bulk Editing Across Many Files at Once
Now this is the most fun part. Let’s combine sed with shell globbing so you can edit many files in one command. This is what got those 40 VPS servers done in ten minutes.
Combining sed with glob (*.conf)
If you want to replace the same string across all .conf files in one folder:
sed -i 's/max_connections = 100/max_connections = 200/g' /etc/mysql/conf.d/*.cnf
This command modifies every file matching *.cnf in that conf.d folder. Test with ls /etc/mysql/conf.d/*.cnf first to confirm the file list is correct, so you don’t accidentally edit files you didn’t intend to.
Bulk editing with find + sed (recursive)
If your configs are scattered across many subfolders, use find to locate the files, then hand them to sed:
find /etc/nginx -name "*.conf" -exec sed -i 's/worker_processes 4;/worker_processes 8;/g' {} \;
The difference from plain glob: find here recurses through every subfolder under /etc/nginx, so it finds all .conf files no matter where they live. This is essential when your directory structures differ between servers.
Bulk editing across multiple servers at once (parallel SSH)
Then the next level: if the same file exists across many servers, combine sed with pssh or parallel-ssh:
pssh -h servers.txt -i "sed -i 's/listen 80/listen 8080/g' /etc/nginx/nginx.conf && systemctl reload nginx"
This edits the config on every server in servers.txt, then reloads nginx. One shot, 40 servers done. But please — use pssh -h servers.txt -i with extreme caution, because one wrong command spreads to every server. Always test on a single server first before scaling up.
Step 4: Patterns I Use Every Day in Production
Here are some sed patterns I use constantly in production. Save these — they’re real, and I’ve tested them over and over.
Replacing words with context
Replace all text containing a certain word, even if you don’t know the exact content. For example, commenting out a specific include line:
sed -i 's/^include php7/include php8/' /etc/nginx/nginx.conf
Here the ^ means start of line. So sed only changes lines that begin with “include php7”. That prevents you from editing other lines that happen to contain a similar word in the middle.
Replacing strings with inconsistent whitespace
Sometimes config files have double spaces or tabs that aren’t consistent. Normalize double spaces into one:
sed -i 's/ */ /g' /etc/php/8.3/fpm/pool.d/www.conf
The pattern * means one space followed by zero or more spaces. So double, triple, or tab spaces get simplified to a single space. Careful though — don’t apply this to files that genuinely need meaningful indentation, like YAML. YAML is space-sensitive, so this will wreck the formatting.
Commenting and uncommenting specific lines
You can quickly comment out (prepend #) or uncomment (remove #) specific lines. This is a common use case for MySQL, PHP, or other services that disable features via comments:
# comment the ssh root login line
sed -i 's/^#PermitRootLogin/PermitRootLogin/' /etc/ssh/sshd_config
# uncomment the line
sed -i 's/^PermitRootLogin/#PermitRootLogin/' /etc/ssh/sshd_config
Notice the patterns differ slightly. To comment a line, you add # at the start (via s/^PermitRootLogin/#PermitRootLogin/). To uncomment, you remove the #. This is a safe way to toggle settings without wrecking the file structure.
Step 5: Delete, Insert, and Other Operations
But hold on — when it comes to bulk editing, sed is about more than just replacing. Sometimes you need to delete lines or insert new ones. These are the sneaky tricks not many people know.
Deleting lines by pattern
# delete all lines containing 'fastcgi_pass'
sed -i '/fastcgi_pass/d' /etc/nginx/sites-available/api.example.com.conf
# delete empty lines
grep -v '^$' file.conf > file-changed.conf
Careful with this one. If you’re not sure, don’t jump straight to -i. Print the matching lines first without editing, so you can see what would get deleted:
sed '/fastcgi_pass/d' /etc/nginx/sites-available/api.example.com.conf | less
If the output looks right, then go ahead with -i. Don’t get cocky — always verify first. Yeah, right.
Inserting lines before or after a pattern
# insert a new line BEFORE the line containing 'server_name'
sed -i '/server_name/i listen 8080;' /etc/nginx/sites-available/example.com
# insert a new line AFTER the line containing 'server_name'
sed -i '/server_name/a listen 8080;' /etc/nginx/sites-available/example.com
i is insert (before), a is append (after). This is awesome when you’re adding new directives to configs en masse, without opening each file one by one.
Step 6: Safe In-Place Editing and Testing
The thing I stress the most here: always test without -i before actually changing a production file. The easiest way is to make a test file first:
cp /etc/nginx/nginx.conf /tmp/nginx-test.conf
sed -i 's/worker_processes 4;/worker_processes 8;/' /tmp/nginx-test.conf
diff /etc/nginx/nginx.conf /tmp/nginx-test.conf
With diff, you can see exactly what changed before touching the original. This is my favorite approach — only a few lines differ, and you’re 100% sure before committing to production. Then move the result over:
cp /tmp/nginx-test.conf /etc/nginx/nginx.conf
And always validate the config syntax before reloading the service. For nginx:
nginx -t
For php-fpm:
php-fpm8.3 -t
For sshd:
sshd -t
This catches errors early. If there’s a syntax error, don’t reload. It’ll tell you a command wasn’t found or the syntax is invalid, and you’ll need to restore the file. Trust me — checking syntax is cheaper than restarting a service that ends up down.
Step 7: Troubleshooting & Command Table
Here’s a troubleshooting table for the common issues you’ll run into when using sed for bulk config editing, so you don’t have to go hunting forever.
| Problem | Cause | Quick Fix |
|---|---|---|
| File doesn’t change / result stays the same | Forgot the -i flag, or pattern doesn’t match | Change sed 's/.../' to sed -i 's/.../'. Verify the pattern with grep first. |
| Only the first line changed | Forgot the g (global) flag | Add g at the end: sed -i 's/foo/bar/g' |
| Accidentally replaced every occurrence | Pattern too broad (e.g., just a number) | Make the pattern more specific, use ^ and $ anchors |
| Sed edited a file you didn’t intend | Glob (*.conf) matched other files | Run ls *.conf first to see the list before sed |
| Service syntax error after editing | Missing one character, or inconsistent spacing | Check syntax (nginx -t), restore from the .bak backup |
| Pattern containing / won’t parse | Slash used as the delimiter | Change the delimiter to | or #: sed -i 's|/etc/foo|/etc/bar|' |
And one more thing that often slips past people. If your pattern contains special characters like a period (.), asterisk (*), or dollar sign ($), sed interprets them as regex. So if you want to match literal text that contains a dot, escape the dot first. For example, changing example.com to example.net, the dots need escaping:
sed -i 's/example\.com/example.net/g' /etc/hosts
Without the backslash before the dot, sed treats the dot as “any character” — and that could match patterns you didn’t intend. It’s a tiny detail, but it’s one that leaves admins confused for hours.
FAQ
Q: Can using sed -i corrupt a file?
Yes, if your pattern is wrong, or you accidentally target a binary file. That’s why you should always test without -i first and always back up the original. sed -i overwrites the original file, so a mistake loses your source data unless you have a backup.
Q: How do I edit config files across many servers at once?
Combine sed with parallel SSH tools like pssh, parallel-ssh, or Ansible. Pick whichever tool you’re most comfortable with. Ansible is safer because it has a dry-run mode and explicit targets; pssh is simpler for quick jobs.
Q: Can sed be used on YAML or JSON files?
Technically yes, but be very careful. YAML and JSON files are sensitive to indentation and structure. One small mistake can cause a total parse error. For complex configs like those, it’s safer to use a format-aware tool — for example jq for JSON — or edit manually with a YAML-aware editor.
Q: What’s the difference between sed -i and sed without -i?
sed without -i just prints the transformed output to the terminal without modifying the original file. sed -i writes the result directly back to the file. So for testing, always use it without -i; use -i only when you want to permanently change the file.
So here’s the bottom line. Sed isn’t just a text-replacement tool — it’s the key to bulk editing configs across many servers quickly and relatively safely, as long as you understand pattern matching and stay disciplined about backups. I do this every day in production, and trust me, the time you save is invaluable. Go ahead and try the steps above, starting with the safest one (testing without -i). If you’re still stuck, check the logs and compare them with the output I showed you. Go for it!