Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 92System · Linux

How to replace text in many files

The database moved to a new host, a domain changed, an image registry was renamed — and the old value is in 40 files. sed -i changes them all in one command. Because it changes files in place, the safe way is: find the files first, preview the change, keep a backup, then check. This how-to does exactly that, and covers the traps (slashes in paths, special characters, macOS).

  • 5 Lessen
  • 14 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

92.1

The quick answer

grep -rlF 'db-old.example.com' /opt/app/config
sed -n 's/db-old\.example\.com/db-new.example.com/gp' /opt/app/config/app.yml
grep -rlZF 'db-old.example.com' /opt/app/config | xargs -0 sed -i.bak 's/db-old\.example\.com/db-new.example.com/g'
grep -rnF 'db-new.example.com' /opt/app/config

1 list the files, 2 preview one file, 3 change all (with .bak backups), 4 check.

92.2

Step by step

  1. Find the files (-l names only, -F plain text, -Z null-separated for safe names with spaces):
grep -rlF 'db-old.example.com' /opt/app/config
  1. Preview — -n … p prints only the changed lines, changes nothing:
sed -n 's/db-old\.example\.com/db-new.example.com/gp' /opt/app/config/app.yml
  host: db-new.example.com
  1. Escape special characters in the search part: . → \., [ → \[, * → \*. Use another delimiter when the text has slashes:
sed -i 's#/var/www/old#/srv/www/new#g' site.conf
  1. Change all files, with a backup (-i.bak keeps file.bak):
grep -rlZF 'db-old.example.com' /opt/app/config | xargs -0 sed -i.bak 's/db-old\.example\.com/db-new.example.com/g'
  1. Review and clean up:
for f in $(find /opt/app/config -name '*.bak'); do diff -u "$f" "${f%.bak}"; done | less
find /opt/app/config -name '*.bak' -delete
  1. In a Git repo — no .bak needed, Git is the backup, and git diff shows everything:
git grep -lz 'registry.old.example.com' | xargs -0 sed -i 's/registry\.old\.example\.com/registry.example.com/g'
git diff --stat
  1. Only on some lines — an address before s:
sed -i '/^\s*image:/ s/:1\.4\.2/:1.4.3/' deploy.yaml

92.3

Check that it worked

grep -rnF 'db-old.example.com' /opt/app/config || echo "old value gone"
grep -rnF 'db-new.example.com' /opt/app/config | wc -l

The old value is gone, the count of the new one is right, and the app starts with the new config (test config syntax: nginx -t, yamllint, the app's own check).

92.4

Common problems

ProblemCauseFix
sed: -e expression … unknown option to 's'The text has / in it.Use another delimiter: s#a#b#g.
More was changed than wanted. in the pattern matches any character.Escape dots (\.); use -w-like anchors or line addresses.
Files with spaces break the commandxargs without null separators.grep -Z + xargs -0.
sed: 1: "…": invalid command code on macOSBSD sed needs an argument after -i.sed -i '' 's/a/b/g', or install GNU sed (gsed).
File owner or SELinux label changedsed -i writes a new file.Check with ls -lZ; restorecon; use --follow-symlinks for links.
A YAML/JSON file brokeQuotes or indentation changed.For structured files use yq/jq, then validate.

92.5

Go further

1How do you see what sed would change before changing files?

sed -n 's/old/new/gp' FILE prints only the changed lines.

2The text to replace contains slashes. What do you do?

Use another delimiter, for example s#old/path#new/path#g.

Alle commando's in dit hoofdstuk