2
2
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,
1Definitely +1 for making me realize, after all those long years, that grep has a -r option... – DevSolar – 2010-04-23T08:03:01.633
"grep -r" is a really good answer. The problem with find | xargs is that if there are many files, you will get an "argument list too long" error message. You can use the -n switch for xargs to avoid this, but then you should set the -H switch for grep so you can be sure that it prefixes the filename. There is one advantage of using "find | xargs" instead of "grep -r": you can make arbitrary filter for the files you want to search. – petersohn – 2010-04-23T08:25:28.437
Ack recognizes C as a filetype. Use "ack 'struct messages_sdd_t' --cc", and it will find all C files, including .h header files. – Andy Lester – 2010-05-03T18:56:01.383
@petersohn:
xargs
does not fail in this way. It uses the system value of maximum argument length and splits accordingly. Runxargs --show-limits
to see what it uses on your specific system. If one needs to usefind
, it is also good to see the-exec command {} +
construct in the manual which will more or less emulatexargs
directly in the find command, with as few sub process invocations as possible. – Daniel Andersson – 2012-05-11T06:54:17.930@ant2009: The given answer is incorrect since it will not search recursively when given file names as arguments like this. It will only search through the
.c
files in the current directory, not sub directories. – Daniel Andersson – 2012-05-11T07:05:27.197