170

grep用于在文件中搜索以查看是否有任何行与给定的正则表达式匹配。但是,我有这种情况 - 我想编写一个匹配文件名本身(而不是文件内容)的正则表达式。我将从系统的根目录运行它,以查找所有与正则表达式匹配的文件。

例如,如果我想查找所有以“f”开头并以 .frm 结尾的Visual Basic表单文件,我将使用正则表达式 -

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

grep 可以这样做吗?如果没有,是否有一个实用程序可以让我这样做?

4

10 回答 10

144

在这种情况下,您需要使用find而不是。grep

您还可以findgrepor结合使用egrep

$ find | grep "f[[:alnum:]]\.frm"
于 2012-04-18T15:02:15.953 回答
96

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.

于 2016-11-15T14:47:26.760 回答
54

正如巴勃罗所说,您需要使用find而不是grep,但无需通过管道find传输到grep. find具有内置的功能:

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

find是一个非常强大的按名称搜索文件的程序,支持按文件类型搜索,深度限制,将不同的搜索词与布尔运算组合,以及对找到的文件执行任意命令。有关更多信息,请参见查找手册页

于 2014-08-11T21:10:27.173 回答
22

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
于 2016-10-26T02:48:18.020 回答
16

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.

于 2020-06-02T04:24:28.903 回答
11
find -iname "file_name"

Syntax:
find -type type_descriptor file_name_here

type_descriptor类型:

f:常规文件

d: 目录

l:符号链接

c:字符设备

b:块设备

于 2015-11-23T16:50:05.687 回答
5

你也可以这样做:

tree | grep filename

这会将 tree 命令的输出通过管道传输到 grep 以进行搜索。这只会告诉你文件是否存在。

于 2016-09-12T12:58:27.080 回答
4
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.

于 2021-01-28T08:07:18.217 回答
-1

Also for multiple files.

tree /path/to/directory/ | grep -i "file1 \| file2 \| file3"
于 2019-02-14T15:28:33.507 回答
-3

No, grep works just fine for this:

 grep -rl "filename" [starting point]

 grep -rL "not in filename"
于 2018-02-06T00:21:36.067 回答