united states of america usa  linkedinfacebook   Call Us Today: (786) 646-6461

Linux: search and replace a text within a file recursively

linux iconThis is a nice little command to search and replace one type of content with another. It recursively parses through directories and files.

Be very careful when running this command, because it can do a lot of damage

Below is an example of replacing all "http" with "https" inside a directory /var/www:

find /var/www/ -type f -print0 | xargs -0 sed -i 's/http:/https:/g'

The variables that you should change:
Directory: /var/www/
What to change: http
What to change to: https

The replacement is done using sed and it matches regular expressions. So you should escape your variables (eg: www.example.com should be www.example.com inside the s/http:/https:/g)

ALT is an IT consulting firm, specializing in support and consulting for Windows, Linux and Apple systems.