Showing posts with label Search and replace files with particular string. Show all posts
Showing posts with label Search and replace files with particular string. Show all posts

Thursday, September 16, 2010

Searching files with particular string in Linux

Let say you want to search all "*.conf" files under  “/home/jboss”  having string “cache” in it and display the output.

# find /home/jboss -name *.conf -type f | xargs grep “cache”
(or)
# find /home/jboss -name *.conf -type f -exec grep -HI "cache" {} \;

Now you have a slightly different requirement: 
You want to search all "*.conf" files under  “/home/jboss”  having string “cache” and replace it with "core".
# perl -p -i -e 's/cache/core/g'  `find /home/jboss -type f -name *.conf`
Another version:
#  find /home/jboss -type f -name *.conf | xargs grep -l cache | xargs perl -p -i -e 's/cache/core/g'