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

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.

This question is related to grep

The answer is


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"

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.


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.


Also for multiple files.

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

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.


No, grep works just fine for this:

 grep -rl "filename" [starting point]

 grep -rL "not in filename"

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


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

find . | grep KeywordToSearch

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

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


The easiest way is

find . | grep test

here find will list all the files in the (.) ie current directory, recursively. And then it is just a simple grep. all the files which name has "test" will appeared.

you can play with grep as per your requirement. Note : As the grep is 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') would also comes to the result set. cheers