Bash: is there a way to search for a particular string in a directory of files? [duplicate]

Possible Duplicate:
Search for a text pattern in linux

In bash, I was wondering if there were any commands that would let you know if a particular string you are looking for exists within the files located in the current directory you are in.

Say you are looking for the function 'toUpperCase()' within a set of C files in your current directory. There are a lot of files, so you wouldn't want to manually open each one up using vim and check for the string 'toUpperCase' because that would take a lot of time. If not a bash command line, is there another method to do this efficiently?


With grep:

grep -R "toUppercase()" *

Or, if you have ack-grep installed, simply:

ack-grep "toUppercase"

If you want to limit the search in C files, with ack-grep:

ack-grep -cc "toUppercase()"

ack-grep can also be installed on OSX but the executable is called just ack.


The standard text search tool is grep.

To look for the string toUpperCase( in all .c files in the current directory:

grep -F 'toUpperCase(' *.c

If you want to look in subdirectories as well:

grep -r --include='*.c' -F 'toUpperCase('

If you're looking for C function calls, then ctags may be a better fit. You run it once on all your source files to create an index of identifiers, then use your editor to navigate between calls. All vi-like editors can use ctags's TAGS file. If you use Emacs, run the similar program etags.


You're using vim. You've got a tool to do this built in.

Predictably, there are seven answers already saying to use grep. But I seem to be the only person thus far who has noticed from your question that you are using vim. As such, although you can use grep from within vim, you can also use vim's built-in tool. This is invoked via the :vimgrep command.

To search "all of the C source files in the current directory for calls to the function toUpperCase()" one types the vim command

:vimgrep "\<toUpperCase\_s*(" *.c

The resulting list of matches is automatically loaded up into the quickfix list, accessible with either of (see the on-line help for the subtle difference)

:copen
:cwin

To find the function definition, rather than calls to it, ctags is the tool, as mentioned in Gilles's answer, in conjunction with the :tjump or :tselect commands.

Why use :vimgrep?

The on-line help (:help grep) enmumerates several of the reasons, which I won't parrot here. Further to those, compare the action of :vimgrep with that of dietbuddha's answer. dietbuddha's command line forks an individual grep process for each individual C source file. It doesn't even employ xargs to reduce that overhead. And you still have to somehow parse the output to invoke your text editor on the relevant source files once it is finished. :vimgrep doesn't fork off multiple additional processes at all, and using the result is simplicity itself. Merely selecting one of the entries in the resultant quickfix list automatically positions the cursor on the relevant line of the relevant source file.

In fact, it does exactly what you wrote you would do by hand, except automatically. It's the automated way of doing those very text editor actions. It loads up the file as if loaded by hand, searches it for a regular expression (using the same regular expression syntax that you are already using elsewhere in vim), records the places where matches occur, and then unloads the file.


Yes, the tool is called grep.

The grep command searches one or more input files for lines containing a match to a specified pattern. By default, grep prints the matching lines.

Basic usage is:

grep something somewhere

Or, more specifically:

grep [some options] pattern file

In a very simple case, you recursively want to search through all files, thus you supply the -r option. If you don't care about uppercase or lowercase, make it case-insensitive with the -i option.

grep -ri "touppercase()" .

If you want only the matching parts, use the -o option.

Grep can use regular expressions, and it has a huge manual.


grep is the standard answer -- using either the -r flag to search recursively or in conjunction with find (discussion and examples in other questions on this site, such as this one)

My preferred choice lately is ack. It's not likely installed on your computer, but you can install using apt-get on Ubuntu or simply download. Links, instructions, and reasons that it's better than grep are available at http://betterthangrep.com/