Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
And what's with a recursive search? –  Christian Ivicevic Jul 4 '11 at 19:20
    
Added to answer. –  pbm Jul 4 '11 at 19:23
5  
@ChristianIvicevic Or you can run grep -r "text" /path/to/directory. –  Gilles Jul 4 '11 at 19:48
    
@Caleb: changed... –  pbm Jul 5 '11 at 15:27
add comment

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.

share|improve this answer
    
Looks interesting, but I don't see the top 10 reasons list. Why not link to it directly? –  Faheem Mitha Jul 4 '11 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 Jul 4 '11 at 21:55
    
@Christian: Ok, I see it, thanks. –  Faheem Mitha Jul 5 '11 at 6:07
add comment

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."

share|improve this answer
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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