Personal tools
You are here: Home / cmgui / Wiki / grep
Log in

Forgot your password?


Sourced originally from Peter but kept in my Misc email folder I now can share this with you all

Having spent the last hour and a half (with the help of Matthew and Karl) finding the find/grep command I needed, I thought I should pass it on since it seems pretty useful (apologies to all those Unix gurus who think it is obvious):

I needed to search all files in the example directories looking for a string "centroid". I wanted a list of the files containing the string, together with the string itself, but not a list of all files parsed.

Karl's solution is:

grep string `find . -name "*"` = lists all occurences of string occurring in all files (Note backwards quote around find command)

which works provided you don't have too many files (else overflows the filename buffer).

Matthew's solution is:

find . -name "*" -exec grep string {} \; = puts all files at & below current directory into {} for grep command
find . -name "*" -print -exec grep string {} \; = as previous but also prints all filenames as it finds them
find . -name "*" -exec grep -l string {} \; = as previous but instead prints only files where string found (but not string itself)
find . -name "*" -exec grep string {}/dev/null \; = as previous but prints filenames where string found and the string itself

so from /product/cmiss/examples:

find . -name "*.com" -exec grep centroid {} /dev/null \;

lists all occurences of 'centroid' in all com files at & below the current directory, giving lines like:

./2/21/218/2184/ de da;c xi centroid old in 65..72 cluster 72

(what on earth is cluster?)

Another way is:

find . -name '*.com' | xargs grep centroid