search and replace string on multiple files from unix terminal search and replace string on multiple files from unix terminal unix unix

search and replace string on multiple files from unix terminal


Try this:

find . -name '*.html' -exec sed -i 's/\.html/\.php/g' "{}" \;

It will find all files in the current and subdirectories that end in .html, and run sed on each of them to replace .html with .php anywhere it appears within them.

See http://content.hccfl.edu/pollock/unix/findcmd.htm for more details.


On my Mac, I had to add a parameter to the -i option: the extension to add to the name (in this case, nothing, so the file is stored in-place).

find . -name '*.html' -exec sed -i '' 's/\.html/\.php/g' "{}" \;


You can certainly use ack to select your files to update. For example,to change all "foo" to "bar" in all PHP files, you can do this from the Unix shell: perl -i -p -e's/foo/bar/g' $(ack -f --php)

Source: man ack