Skip to content
Log In Sign Up info@muvcloud.com

Replace Text with sed

sed 's/old/new/' file.txt
sed 's/old/new/g' file.txt
sed -i.bak 's/old/new/g' file.txt
sed 's|/var/www/old|/var/www/new|g' site.conf

The first line replaces the first old on each line and prints the result to the screen; the file stays unchanged. The g flag in the second replaces every match on each line. The third edits the file itself and keeps the original as file.txt.bak. The fourth uses | as the delimiter, so the slashes in the paths need no escaping. sed on all current distributions is GNU sed.

How the s Command Works

The form is s/pattern/replacement/flags. The pattern is a regular expression, the replacement is text, and the flags change how it applies:

FlagEffect
gEvery match on the line
2Only the second match
IIgnores case
pPrints the changed line

Without g, sed replaces only the first match on each line, which is the most common surprise. With I, a GNU extension, s/error/ERROR/gI also changes Error.

Preview the Change First

sed -n 's|http://example.com|https://example.com|gp' site.conf

-n turns off the normal output, and the p flag prints each line that changed, so you see exactly which lines the edit touches. Run this before any in-place edit.

Edit the File in Place, with a Backup

Warning: -i rewrites the file, and there is no undo. A loose pattern can change lines you did not mean to touch. Always keep a copy with -i.bak, and for important configuration take a snapshot or backup as well.

sudo sed -i.bak 's/listen 80;/listen 8080;/' /etc/nginx/sites-available/example.com
diff /etc/nginx/sites-available/example.com.bak /etc/nginx/sites-available/example.com
sudo nginx -t

diff prints only the lines that differ; grep -n for the new text works too. Test the configuration with the service's own check, here nginx -t, before you reload, and delete the .bak file only when everything works. Mind where the backup lands. Nginx on Debian and Ubuntu loads every file in sites-enabled, so a .bak file there runs as a second configuration; Apache there and Nginx on AlmaLinux and Rocky Linux load only names that end in .conf, so they skip it. -i writes a new file in place of the old one, so a symbolic link, such as the entries in sites-enabled, is replaced by a regular file unless you add the GNU option --follow-symlinks. Edit the file in sites-available instead.

Other Delimiters and Escaping

sed 's/\/var\/www/\/srv\/www/g' file
sed 's|/var/www|/srv/www|g' file
sed 's/[0-9][0-9]*/(&)/g' file
sed 's/R&D/R\&D team/' file

The first two lines do the same thing; the delimiter is whatever character follows s, and a slash inside the text must be escaped only when the slash is the delimiter. In the replacement, & stands for the whole match, so the third line puts every number in parentheses; a literal ampersand is written \&, as in the fourth line. In the pattern, ., *, [, ^, and $ have special meanings; write \. for a literal dot.

Limit the Change to Some Lines

sed '10,20s/foo/bar/g' file
sed '/server_name/s/old\.example\.com/new.example.com/' file
sed '/^#/!s/foo/bar/g' file
sed '$s/foo/bar/' file

A line range or a pattern before s limits where it applies: lines 10 to 20, lines that contain server_name, every line that does not start with # (the ! negates), or the last line ($). A range between two patterns, /start/,/end/, also works.

Groups and Extended Syntax

sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\3.\2.\1/g' dates.txt

With -E, parentheses capture parts of the match, and \1, \2, and \3 insert them in the replacement. This line turns 2026-11-28 into 28.11.2026. Without -E, groups need backslashes: \( and \).

Replace in Many Files

grep -rl 'http://example.com' /var/www/site
find /var/www/site -type f -name '*.html' -exec sed -i.bak 's|http://example.com|https://example.com|g' {} +

List the files that contain the text with grep first, and take a backup or snapshot of the folder. The find command then edits every matching file and leaves a .bak copy next to each one. Check a few with diff, then remove the backups; the find command guide shows how to delete safely. To search before you edit, see how to grep for multiple strings; for edits by column, awk is the better tool.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations