Find words in many files

Posted on

Problem :

I am looking for this struct messages_sdd_t and I need to search through a lot of *.c files to find it. However, I can’t seen to find a match as I want to exclude all the words ‘struct’ and ‘messages_sdd_t’. As I want to search on this only ‘struct messages_sdd_t’ The reason for this is, as struct is used many times and I keep getting pages or search results.

I have been doing this without success:

find . -type f -name '*.c' | xargs grep 'struct messages_sdd_t'

and this

find . -type f -name '*.c' | xargs egrep -w 'struct|messages_sdd_t'

Many thanks for any suggestions,

Solution :

Use ack:

ack “struct messages_sdd_t” *.c

It is recoursive by default and it is a lot faster than grep (according to my observation) when searching trough a directory containing tons of source code.

If you don’t want to use ack, grep should be fine too:

grep -r “struct messages_sdd_t” *.c

find . -type f -name '*.c' -exec grep 'structs+messages_sdd_t' {} ;

Or if you only care about the filename:

find . -type f -name '*.c' -exec grep -q 'structs+messages_sdd_t' {} ; -print

grep -R --include='*.c' 'struct messages_sdd_t' .

This will search recursively and use grep itself to pick only .c files.

Consider adding -H to always write the file name with the line match.

Leave a Reply

Your email address will not be published. Required fields are marked *