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.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket