Search This Blog

Monday, June 15, 2009

Using 'find' and 'exec'

I needed to go through all of the files in my project workspace just recently and in particular I was looking for certain string. Now in Windoze world I would've done a GUI search for text in file. How do I do the same in Linux world. Well this is one way:

find . -exec grep "BLABLABLA" '{}' \; -print

What this does is as follows:
  1. First find all files starting at '.', ie: at my current location
  2. For every file that I find execute the command grep "BLABLABLA" '{}', where '{}' contains the results of the find command (in this case a listing of every file path.
  3. 'print' out any resulting files that have this text in it.
Sweet. All done and the results I get are something along the lines of:

mkopka@random:~/$ find . -exec grep "year.development" '{}' \; -print
<center>Copyright © @year.development@ TerraPages International</center>
./src/html/common/footer.jsp.in
<filter token="year.development" value="2006, 2007" />
./build.xml


Which shows me that there are two files 'footer.jsp.in' and build.xml which have the text that I am looking for, and it also gives me the path to each file and the context of the text Im searching for is printed out.

Some links to more info:
- http://en.wikipedia.org/wiki/Find
- http://www.ibm.com/developerworks/aix/library/au-unix-find.html

No comments:

Post a Comment