Replacing text in a Linux file with sedIt’s very convenient. It’s also the kind of command that can corrupt a configuration file in a second if you run it directly. sed -i poorly reread.
I advise you to adopt a simple habit: read before writingWe test the substitution on standard output, redirect to a temporary file, check the diff, and only then replace the original. It’s a bit longer, but on a server, it avoids silly repairs.

Understanding what sed actually changes
sed It reads a stream of text, applies a rule, and then displays the result. By default, it does not modify the source file. This is precisely the behavior to use when testing a command.
sed 's/old/new/' file.conf
This command replaces the first occurrence of ancient by new on each displayed line. The file file.conf remains intact. So you can watch the result without taking any risk.
If you want to replace all occurrences on a line, add the flag g :
sed 's/old/new/g' file.conf
The classic pitfall is to confuse the two. Without g`sed` only replaces the first occurrence of each line. With gIt replaces all occurrences found on the line.
Test in a new file before touching the original.
For a configuration file, I prefer to create a test file. You keep the original in front of you and can compare at your leisure.
sed 's/old/new/g' file.conf > file.conf.new
diff -u file.conf file.conf.new
THE diff -u It shows you exactly which lines have been changed. If the result is correct, you can replace the old file.
move file.conf.new file.conf
For sensitive files, always make a dated copy before handling them:
cp fichier.conf fichier.conf.bak-$(date +%F-%H%M)
It’s not a true backup strategy, but it’s often enough to revert to the previous state after a minor replacement error.
Use sed -i without playing with fire
The option -i It modifies the file in place. It’s convenient, but I would never run it on an important file without a backup or prior testing.
The minimum requirement is to ask sed to create a backup copy:
sed -i.bak 's/old/new/g' file.conf
You then get the modified file, plus a copy file.conf.bakIf the substitution broke something, you can quickly restore:
mv fichier.conf.bak fichier.conf
On some distributions or on macOS, the behavior of sed -i may vary. On Linux GNU sed, -i.bak works well. If you are writing a procedure intended for multiple systems, always test it on a copy.
Replacing a path without struggling with slashes
When the chain contains /The classic syntax quickly becomes unreadable:
sed 's#/var/www/html#/srv/www/site#g' nginx.conf > nginx.conf.test
Here, I use # as a separator instead of /The command remains clearer and you avoid a series of tedious escaping operations. This is very useful for paths, URLs, or certain configuration variables.
Before applying this change to an Nginx, Apache, or application file, check the affected lines:
diff -u nginx.conf nginx.conf.test
grep -n '/srv/www/site' nginx.conf.test
If you modify a service configuration, also perform a service-specific test before restarting. Sed replaces text; it does not validate your application’s syntax.
Show only the relevant rows
To inspect a file before modification, you can combine sed And grepIf you want to display a range of rows:
sed -n '1,80p' file.conf
THE -n avoids displaying the entire file. p It only prints the requested range. This is useful for controlling a specific area in a long configuration.
To display the lines that match a pattern:
sed -n '/server_name/p' nginx.conf
If your primary need is to search within files, grep often remains more naturalSed becomes interesting when you need to transform the result, not just find it.
Modify multiple files with caution
Sed can quickly tempt you to modify an entire folder. In this case, I advise you to slow down. Start by listing the files that actually contain the string you’re targeting:
grep -rl 'old' /etc/my-app
Next, run a loop that saves each file before replacing it:
grep -rl 'old' /etc/my-app | while read -r file; do
cp "$file" "$file.bak"
sed -i 's/ancien/nouveau/g' "$file"
done
This is not a command to copy blindly onto /etcAdapt the folder, check the list with grep -rlThen work on a limited scope. To search for files before taking action, you can also use find on Linux.
Post-replacement checks
After a substitution, don’t just accept that the order has been placed. At least check these points:
- the presence of the new value with
grep -n 'new' file.conf; - the absence of the old value with
grep -n 'old' file.conf; - the difference between the original and the modified file if you kept a copy;
- the syntax test of the application in question, when it exists;
- the service logs after reloading or restarting.
For logs, keep in mind to monitor what’s happening live with tail Or journalctl -fSed can correct a value, but only the service will tell you if the configuration remains valid.
When to avoid sed
Sed is perfect for straightforward substitution in a text file. I would avoid using it to modify a structured file if order, blocks, or quotation marks are critical. For JSON, complex YAML, XML, or highly sensitive configurations, a dedicated tool or a small script might be safer.
If you are preparing a migration or a fix affecting multiple files, make a full backup beforehand. An archive or local copy can be a temporary solution, but for a true, regular backup, use a reliable method such as rsync.
To go further, the GNU sed manual and the page sed(1) from man7.org What remains are the useful references. They are less convenient than a practical example, but they quickly clarify the options when you move beyond simple replacements.