Updated: 28 September 2026 · Applies to: Linux (GNU grep and sed)

Replace a word or URL in every file of a folder with one command, for example after changing a domain.

Steps

  1. Back up the folder first: tar czf ~/backup-site.tar.gz ~/public_html.
  2. List the files that contain the text:
    grep -rl 'old-text' ~/public_html
  3. Replace it in all of them:
    grep -rl 'old-text' ~/public_html | xargs sed -i 's/old-text/new-text/g'

Note: Characters such as / or . have special meaning in sed. For URLs, use another separator: sed -i 's|https://old.com|https://new.com|g'. For WordPress databases use wp search-replace instead.

Ucartz services for this topic

Was this answer helpful? 0 Users Found This Useful (0 Votes)