Find secrets – undocumented feature

Problem

You want to find a pattern in a files, but have the file name and pattern printed.

If you run find . -type f -exec grep -i “pattern” {} ; it will only show the pattern turning up – i.e not the file name as well.

If you run find . -type f -exec grep -il “pattern” {} ; it will only show the file name turning up – i.e not the line containing the pattern as well! 🙂

Solution

Maybe you have seen this solution before, but AFAIK it is completely undocumented.

I discovered it whilst trying to do this very thing and I just thought, hmmm … I wonder. 🙂

Example

To force grep to print the line containing the pattern and the file name, we pass /dev/null as another argument. Find sees this as multiple arguments, so prints the file name and line containing pattern.