I am trying to find out the file in which the following flag is used - TRACE_WANTED". However, I don't want the find to search this flag in .c and .h. How can I issue a command to exclude the find from *.c and *.h files.

Here is the typical command I am using -

find ./ -iname  *.c -exec grep -iHrn  TRACE_WANTED {} \;

GNU grep has the ability to exclude globs from its recursive searches built in. Try:

grep -iHrn  --exclude='*.c' --exclude='*.h' TRACE_WANTED

This searches recursively starting from the current directory, just like your find command. It excludes all *.c and *.h files.

| improve this answer | |

If all you need is to find the files which contains the word TRACE_WANTED, excluding *.c and *.h files, you don't even need to use find, grep alone can do this work for you:

 grep -r --exclude={"*.c","*.h"} TRACE_WANTED ./

The quotes are unnecessary in bash, but necessary in zsh by default.

| improve this answer | |
find . ! -name '*.[ch]' -type f -exec grep -l TRACE_WANTED {} +

Or if you can use a grep with -recursive search:

grep -lr --exclude='*.[ch]' TRACE_WANTED
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.