Personal tools
You are here: Home cmgui Wiki grep
Views
FrontPage >> General Computing Links >> Unix tips and tricks >>

grep

last edited 4 years ago by blackett

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/example_2184.com:fem 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

Contributing to this site

Please add to the wiki any relevant information that you think might be useful to other users of this website. For example, you might like to contribute your experiences, questions and answers.

You are encouraged to contribute to this site regardless of your level of experience. Contributions are welcomed from new and regular visitors.

If you ask a question and receive an answer from a developer you should record it in the wiki. This information is extremely useful and can help other users overcome the same problem.

See how to add and edit pages for more information.