How do I search files, some of which might be directories (in this case recursively) for a given string?
I'd like to search source files for a class definition, so it would also be great if there was the name of the containing file before each output string.
You could use grep:
This will also print the line number next to each match.
Case insensitive:
Search non-recursively in all cpp files:
All the above answers will work perfectly well, but may cause false positives if you've got the string in more than just source code. Say you are looking for where you use the FooBar class in a java project, you can search for all files named *.java and grep just those.
The section in between the $() will expand to the output of that command. The -H prints out the filename. It's the default where there is more than one file name, but we explicitly add it here in case the expansion only returns one file name. The -n prints the line number of the match.
This will work for small projects, but if you're dealing with a larger project, there the potential for the find to expand to a string longer than the command line limit. It's therefore safer to use:
xargs will take input from standard in and split it up so it fits on the command line, running the command multiple times if necessary. The -print0 and -0 are required to deal with files with spaces in their name.
You can also look for all the filesnames with a match in using:
The -l will just display the file name and stop looking in a particular file as soon as it finds a match.
If you're going to be doing this sort of thing frequently, you may want to look at a tool like ctags or exuberant tags, which maintain a search index of your source code and can answer these sorts of questions quickly. Both vim and emacs have support for these tools. You may also like to look at an IDE like eclipse, which can do cross referencing very well.
(I know you were looking for a class definition, but in Java a class will be defined in a file with the same name as the class. If you were doing cpp, you could do something like:
For searching through source code, I've found that ack (betterthangrep.com) is, to quote the marketing, better than grep. Its output is easier to read and it will do a lot of stuff automagically, like ignoring non-source code files: VCS directories (eg CVS and .svn), backup files.
ack is better for searching source codes as it already recognize some of the most popular file extension (c, perl, etc). It's as fast as grep and by default ignoring all your version control artifact. For example if you want to search some pattern perl source code you could use
instead of
finds all files containing the characters "string" in all (non-hidden) files in the current directory and all sub-directories recursively. You can use the usual regexes to tune the string. See the grep info/man pages for more options.
Already answered (I think this is what you are looking for) on this serverfault post:
Recursive text search with grep and file patterns
this is something I find myself using alot so I have put two bash functions in my
.bashrc
First one does a recursive search for text in filenames.
Second one will do a recursive search for text inside of files returning the filenames of the files that match.
example usage:
This classical find + exec could also be handy:
Try the command:
It will work !!