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
- Back up the folder first:
tar czf ~/backup-site.tar.gz ~/public_html. - List the files that contain the text:
grep -rl 'old-text' ~/public_html
- 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
- 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.
- Linux server management: hardening, patching, monitoring and automation for Ubuntu, Debian, RHEL and AlmaLinux.
