grep用于在文件中搜索以查看是否有任何行与给定的正则表达式匹配。但是,我有这种情况 - 我想编写一个匹配文件名本身(而不是文件内容)的正则表达式。我将从系统的根目录运行它,以查找所有与正则表达式匹配的文件。
例如,如果我想查找所有以“f”开头并以 .frm 结尾的Visual Basic表单文件,我将使用正则表达式 -
"f[[:alnum:]]*\.frm"
grep 可以这样做吗?如果没有,是否有一个实用程序可以让我这样做?
grep用于在文件中搜索以查看是否有任何行与给定的正则表达式匹配。但是,我有这种情况 - 我想编写一个匹配文件名本身(而不是文件内容)的正则表达式。我将从系统的根目录运行它,以查找所有与正则表达式匹配的文件。
例如,如果我想查找所有以“f”开头并以 .frm 结尾的Visual Basic表单文件,我将使用正则表达式 -
"f[[:alnum:]]*\.frm"
grep 可以这样做吗?如果没有,是否有一个实用程序可以让我这样做?
在这种情况下,您需要使用find
而不是。grep
您还可以find
与grep
or结合使用egrep
:
$ find | grep "f[[:alnum:]]\.frm"
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.
正如巴勃罗所说,您需要使用find
而不是grep
,但无需通过管道find
传输到grep
. find
具有内置的功能:
find . -regex 'f[[:alnum:]]\.frm'
find
是一个非常强大的按名称搜索文件的程序,支持按文件类型搜索,深度限制,将不同的搜索词与布尔运算组合,以及对找到的文件执行任意命令。有关更多信息,请参见查找手册页。
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
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.
find -iname "file_name"
Syntax:
find -type type_descriptor file_name_here
type_descriptor类型:
f:常规文件
d: 目录
l:符号链接
c:字符设备
b:块设备
你也可以这样做:
tree | grep filename
这会将 tree 命令的输出通过管道传输到 grep 以进行搜索。这只会告诉你文件是否存在。
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.
Also for multiple files.
tree /path/to/directory/ | grep -i "file1 \| file2 \| file3"
No, grep works just fine for this:
grep -rl "filename" [starting point]
grep -rL "not in filename"