How can I grep in source files for some text?
Solution 1
ack (or, on Debian/Ubuntu, ack-grep) will ignore non-source files like version control or binaries. Very useful.
to search just .c and .h files, as above:
ack-grep -i --cc "invalid preset"
the --cc
(the longer form is --type cc
) only looks at .c .h & .xs files. The full list of filetypes is viewable with ack-grep --help type
. Most of the time, you won't particularly need the --type
, as it will generally only have the files to search, and then files you won't see by default, like binaries, backups and version control files.
Solution 2
The grep
program itself can search recursively and also accepts an option to search only certain files. The following is equivalent to your two find
commands.
grep -Ri --include=*.[ch] invalid\ preset .
Solution 3
The find command can call grep itself.
find . \( -name "*.c" -o -name "*.h" \) -exec grep -i "invalid preset" {} \; -print
and variations of thereof.
Related videos on Youtube
wim
Hi from Chicago! Python dev with interest in mathematics, music, robotics and computer vision. I hope my Q&A have been helpful for you. If one of my answers has saved your butt today and you would like a way to say thank you, then feel free to buy me a coffee! :-D [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo *Click*
Updated on September 18, 2022Comments
-
wim over 1 year
At the moment I'm using two commands, I'm sure there must be a better way...
wim@wim-acer:~/ffmpeg$ find . -name "*.h" -print0 | xargs -0 grep -i invalid\ preset wim@wim-acer:~/ffmpeg$ find . -name "*.c" -print0 | xargs -0 grep -i invalid\ preset
-
Admin over 12 yearsdoes
-name '*.[ch]'
work?
-
-
Keith over 12 yearsChanged it to the obvious variation. ;-)
-
Mike over 8 yearsThis worked for me when I got rid of the brackets