I have many files in a specific repository and i must find a file which has a specific string in its content (actually referencing the file Version.cc.in). What is the best solution to find it?
4 Answers
You could use grep:
grep "text" /path/to/directory/*
For recusive search you could use -r option for grep:
grep -r "text" /path/to/directory/*
or ** in path:
grep "text" /path/to/directory/**/*
but, availability of ** operator is shell dependent - as far I know it is in zsh and bash (4 only?), it may not be available in other shells.
-
And what's with a recursive search?Christian Ivicevic– Christian Ivicevic2011-07-04 19:20:19 +00:00Commented Jul 4, 2011 at 19:20
-
-
5@ChristianIvicevic Or you can run
grep -r "text" /path/to/directory.Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2011-07-04 19:48:34 +00:00Commented Jul 4, 2011 at 19:48 -
You can use a tool called ack. It is descibed as:
ack is a tool like grep, designed for programmers with large trees of heterogeneous source code.
Basically it is similar to grep in that it searches files for patterns, but with a few key differences. Namely, it searches recursively and ignores version control and backup files by default (e.g. CVS, .svn, foo~, #foo#). And since it is built with Perl it is cross-platform. It also allows you to specify particular file types within a directory to search, so if you only want to search Perl files you could type
ack --perl pattern
If interested there is a list of the "top 10 reasons to use ack instead of grep" on the ack homepage. Plus, there is a Vim plugin.
-
Looks interesting, but I don't see the top 10 reasons list. Why not link to it directly?Faheem Mitha– Faheem Mitha2011-07-04 21:27:45 +00:00Commented Jul 4, 2011 at 21:27
-
1@Faheem Mitha: Scroll down till Top 10 reasons to use ack instead of grep. where 13 reasons are listed.Christian Ivicevic– Christian Ivicevic2011-07-04 21:55:48 +00:00Commented Jul 4, 2011 at 21:55
There is another tool similar to Ack, called The Silver Searcher. The developer claims that it "is like ack, but better. It’s fast. It’s damn fast. The only thing faster is stuff that builds indicies beforehand, like Exuberant Ctags."
If you just want to find and list the file, not the actual contents, then use the -l option with grep:
grep -lr Version.cc.in $(pwd)/*
Which will give you a list of all files and the absolute path to them (assuming the files are in directories/subdirectories of your current working directory).