Combine Linux find and grep with exec

Summary: How to use the Linux find command with the grep option, using the find "exec" argument.

One of the really terrific things about the Linux find command is that you can combine it with the regular Linux grep facility. This lets you search for text strings and regular expressions in many directories at one time.

A simple grep command

The Linux grep command lets you search multiple files, so if you're looking for the proverbial needle in a haystack, you could issue a grep command like this:

grep 'needle' *

This searches every file in the current directory for the pattern 'needle'. But if you want to search files in many different directories, that's where you need to combine the find and grep commands.

Combining find and grep

Jumping right to the solution, if you want to search every subdirectory beneath the current directory for the string 'needle', you'd issue a find/grep command like this:

find . -type f -exec grep 'needle' {} \;

Looking at this command one argument at a time, we have:

  • We begin with the 'find' command itself.
  • The '.' character means "search in the current directory, and all sub-directories below this directory".
  • I use the "-type f" argument to tell the find command to only look at files. It doesn't make sense to use the grep command on directories.
  • The find command 'exec' argument lets you execute a command, in this case the grep command.
  • The "grep 'needle'" part of the command looks like a normal grep command.
  • Our find/exec/grep command ends with the unusual syntax "{} \;". This is the find command syntax that helps you know that you're about to feed the grep command a lot of files. The most important thing to know about this syntax is that you need it, and if you don't supply it, you'll get an error message.

I hope breaking that find/grep command down like that is helpful.

Other find, exec, and grep commands

To wrap this up, there are two other options I use with this find/grep command solution. First, if you want to perform a case-insensitive search, just add the "-i" flad to the grep command, like this:

find . -type f -exec grep -i 'needle' {} \;

Next, because of what the find and grep commands are doing for you here, it will probably be very helpful to add the "-l" flag to the grep command, like this:

find . -type f -exec grep -l 'needle' {} \;

This tells the grep command to list the names of the files where the text pattern has been found. Again, because the find command is scouring many different files and directories with the grep command, this grep argument is very commonly used with this command.

More Linux find command information

For more information on the find command, please see the Linux find command tutorial on my alvinalexander.com website. That tutorial includes many of the most commonly needed find command examples, including other find/grep examples.

Linux find, grep, and exec - Summary

I hope this short Linux find/grep command tutorial has been helpful. As you can see from these simple examples, the find and grep commands can be combined for some very powerful file-searching capabilities.