For school I have to do an assignment in which I need to use the grep
command within minix to search the source tree for files containing "int main(". With the source tree being stored in /usr/src
, I have the following command:
grep -rn --include=\*.c /usr/src/ "int main("
The problem is that I can't get the command to only search in .c
files.
So the question is: how do I search, using grep
, for any files containing "int main("
while only searching in .c
files?
Any help is very much appreciated.
I think this following will work:
grep "int main(" $(find /usr/src|grep .c)
This will take all the files in /usr/src, and see which ones have the string ".c" in their filenames, and then look for some string withing each of those files. The grep withing the $( will not look inside of those files, because it looks in searches for whatever string in stdin, which it receives through the pipe.
EDIT:
This might not work, depending on the level of power your computers have. On my laptop it returns "zsh: argument list too long: grep". But the principle should work.
EDIT EDIT:
If you want to search (for example) also in, say, .h files, you could use the following:
grep "int main(" $(find /usr/src|grep .c|grep .h)