163
votes

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 Visual Basic 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?

10
The must be a lot of duplicates from the first nearly four years.Peter Mortensen
Not a duplicate (controlling the output from grep (independent of the matching, etc.)): How can I use grep to show just filenames on Linux?Peter Mortensen

10 Answers

139
votes

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"
88
votes

Example

find <path> -name '*FileName*'

From manual:

find -name pattern

Base of file name (the path with the leading directories removed) matches shell pattern pattern. Because the leading directories are removed, the file names considered for a match with -name will never include a slash, so "-name a/b" will never match anything (you probably need to use -path instead). The metacharacters ("*", "?", and "[]") match a "." at the start of the base name (this is a change in find‐ utils-4.2.2; see section STANDARDS CONFORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -path. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell.

52
votes

As Pablo said, you need to use find instead of grep, but there's no need to pipe find to grep. find has that functionality built in:

find . -regex 'f[[:alnum:]]\.frm'

find is a very powerful program for searching for files by name and supports searching by file type, depth limiting, combining different search terms with boolean operations, and executing arbitrary commands on found files. See the find man page for more information.

21
votes

You can find the relative path of a file using tree. Just pipe the output to grep to filter down:

tree -f | grep filename

Here is a function you can put into your .bash_profile, .bashrc, .zshrc or other...

findfile(){ tree -f | grep $1; } # $1 = filename, -f is full path
14
votes

The easiest way is

find . | grep test

Here find will list all the files in the (.), i.e., the current directory, recursively.

And then it is just a simple grep. All the files which name has "test" will appear.

You can play with grep as per your requirement. Note: As the grep is a generic string classification. It can result in giving you not only file names. But if a path has a directory ('/xyz_test_123/other.txt') it would also be part of the result set.

10
votes
find -iname "file_name"

Syntax:
find -type type_descriptor file_name_here

type_descriptor types:

f: regular file

d: directory

l: symbolic link

c: character devices

b: block devices

5
votes

You can also do:

tree | grep filename

This pipes the output of the tree command to grep for a search. This will only tell you whether the file exists though.

1
votes
find . | grep KeywordToSearch

Here . means the current directory which is the value for the path parameter for the find command. It is piped to grep to search the keyword which should return all matching results.

Note: This is case sensitive. So for example fileName and FileName are not same.

-1
votes

Also for multiple files.

tree /path/to/directory/ | grep -i "file1 \| file2 \| file3"
-3
votes

No, grep works just fine for this:

 grep -rl "filename" [starting point]

 grep -rL "not in filename"