Linux Find Replace in Multiple Files Using grep and sed

Here is a quick tip if you need to recursively grep a directory for files containing a certain string and replace the entire line containing that string here's one way to accomplish that.


grep -lr wngzhxknyp * | xargs sed -i 's/.*wngzhxknyp.*/<?php/'


The -lr switches make grep searche the current directory recursively (the -r) and only returns the filenames (the -l) that contain the matching string "wngzhxknyp". When we pipe the resulting filenames to sed we use sed to search and replace any lines containing "wngzhxknyp" with "<?php".



Boise Web Design

Established in the City of Trees in Boise Idaho in 2002, Vector Network Solutions is now a leading web development company providing Boise web design with over ten years of experience. Leading the way with a proven track record on the industries top web platforms, VectorNS is capable of delivering exceptional solutions to clients around the globe. Our strategic methods afford us the ability to provide Boise with web design, development and a sharp competitive edge. Call or e-mail today to start initiating your project with the experienced professionals at VectorNS.

Free Web Tips & Ideas!

Sign up for VectorNS Web Tips and recieve occasional tips & ideas to enhance your web presence.