Search a string in multiple files recursively

Thursday, January 17, 2013 , , 2 Comments

Almost every unix programmer will need this at least once in a day.
For searching a string abc in all the files in the current direcory we use:
grep 'abc' *

If you want to serach in files which are under any sub directories also including the files in the current directory then we have to combine both find and grep:

find . -type f|xargs grep 'abc'

Another possible way is using exec of find command:
find . -type f -exec grep 'abc' {} \;

2 comments:

  1. And how would i do it ..if I want to delete all those files with matching strings?

    ReplyDelete
  2. find . -type f|xargs perl -i -lne 'print if(/abc/)'

    ReplyDelete