How to "grep" for a filename instead of the contents of a file?

CodeBlue picture CodeBlue · Apr 18, 2012 · Viewed 366k times · Source

grep is used to search within a file to see if any line matches a given regular expression. However, I have this situation - I want to write a regular expression that will match the filename itself (and not the contents of the file). I will run this from the system's root directory, to find all those files that match the regular expression.

For example, if I want to find all VB form files that start with an "f" and end with .frm, I'll use the regular expression -

   "f[[:alnum:]]*\.frm"

Can grep do this? If not, is there a utility that would let me do this? Thanks.

Answer

Pablo Santa Cruz picture Pablo Santa Cruz · Apr 18, 2012

You need to use find instead of grep in this case.

You can also use find in combination with grep or egrep:

$ find | grep "f[[:alnum:]]\.frm"