24 June, 2011

On a number of occasions I found it necessary to locate a list of files by extension (e.g. header/source files) and found that if I needed to locate files of multiple extensions I took the 'cowards way out', executing two 'find' commands and appending the results to a temporary output file, then using the output file as input to the next command in the pipeline (e.g. grep).

I took the time to find the more appropriate method this morning, the ability to execute a find with multiple regular expressions.


find . -type f \( -name "*.c*" -o -name "*.h*" \) -exec grep -l SetEvent {} \;


The above command will locate all header and implementation files (e.g. *.h, *.hpp, *.c, *.cpp...) and return a list of files that contain the SetEvent expression within them. Note the parenthesis are significant to ensure the or'd list is piped to the exec command, otherwise only the second extension list with run through the exec command.

Cheers.