Find files containing a pattern

You can find files with grep or find with grep.

Using grep

grep -riHn [pattern] .

This will recursively find all files under the current directory (and it’s subdirectories) that contain the given pattern.

The flags are as follows:

  • r recursive search.
  • i ignore character case.
  • H includes the filenames in the output.
  • n shows line numbers.

Using find and grep

find [path] -name [filename] -exec grep -iHn [pattern] {} \;

This will recursively search the directory [path] for files named [filename] that contain the [pattern]. For each match found, it will output the line containing the pattern.

The flags are as above.

For example, find . -name "*.html" -exec grep -iHn mindSpill {} \; will recursively search the current directory for any HTML files containing the word “mindspill”.

You can leave off the -name argument to search all files in the given directory.

Another useful grep flag is l to suppress normal output and instead print the name of each input file from which output would normally have been printed.


The combination of the find command, pipes and xargs is a very useful one - it can be used to perform a command on/with each of the found files. See xargs for details of how to use xargs.

Last modified: 19/02/2007 Tags: , ,

Related Pages

Other pages possibly of interest:

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top