[英]How can I recursively find all files in current and subfolders based on wildcard matching?
How can I recursively find all files in current and subfolders based on wildcard matching?如何根据通配符匹配递归查找当前文件夹和子文件夹中的所有文件?
Piping find into grep is often more convenient;管道 find 到 grep 通常更方便; it gives you the full power of regular expressions for arbitrary wildcard matching.它为您提供了用于任意通配符匹配的正则表达式的全部功能。
For example, to find all files with case insensitive string "foo" in the filename:例如,要查找文件名中包含不区分大小写字符串“foo”的所有文件:
~$ find . -print | grep -i foo
find
will find all files that match a pattern: find
将查找与模式匹配的所有文件:
find . -name "*foo"
However, if you want a picture:但是,如果你想要一张图片:
tree -P "*foo"
Hope this helps!希望这可以帮助!
fd
In case, find
is too slow, try fd
utility - a simple and fast alternative to find
written in Rust.如果find
太慢,请尝试fd
实用程序 - 一个简单而快速的替代find
用 Rust 编写的。
Syntax:句法:
fd PATTERN
Demo:演示:
Homepage: https://github.com/sharkdp/fd主页: https ://github.com/sharkdp/fd
find -L . -name "foo*"
In a few cases, I have needed the -L parameter to handle symbolic directory links.在少数情况下,我需要 -L 参数来处理符号目录链接。 By default symbolic links are ignored.默认情况下,符号链接被忽略。 In those cases it was quite confusing as I would change directory to a sub-directory and see the file matching the pattern but find would not return the filename.在这些情况下,这很令人困惑,因为我会将目录更改为子目录并查看与模式匹配的文件,但 find 不会返回文件名。 Using -L solves that issue.使用 -L 解决了这个问题。 The symbolic link options for find are -P -L -H find 的符号链接选项是 -P -L -H
If your shell supports a new globbing option (can be enabled by: shopt -s globstar
), you can use:如果您的 shell 支持新的 globbing 选项(可以通过: shopt -s globstar
启用),您可以使用:
echo **/*foo*
to find any files or folders recursively.递归查找任何文件或文件夹。 This is supported by Bash 4, zsh and similar shells.这受到 Bash 4、zsh 和类似 shell 的支持。
Personally I've got this shell function defined:我个人已经定义了这个 shell 函数:
f() { find . -name "*$1*"; }
Note: Above line can be pasted directly to shell or added into your user's ~/.bashrc
file.注意:上面的行可以直接粘贴到 shell 或添加到用户的~/.bashrc
文件中。
Then I can look for any files by typing:然后我可以通过键入以下内容来查找任何文件:
f some_name
Alternatively you can use a fd
utility with a simple syntax, eg fd pattern
.或者,您可以使用具有简单语法的fd
实用程序,例如fd pattern
。
Use利用
find path/to/dir -name "*.ext1" -o -name "*.ext2"
Explanation解释
find
does recursion.默认情况下find
进行递归。-o
stands for -or
. -o
代表-or
。 So above means search for this wildcard OR this one.所以上面的意思是搜索这个通配符或这个。 If you have only one pattern then no need for -o
.如果您只有一种模式,则不需要-o
。find <directory_path> -type f -name "<wildcard-match>"
在通配符匹配中,您可以提供您希望匹配的字符串,例如 *.c(适用于所有 c 文件)
You can use:您可以使用:
# find . -type f -name 'text_for_search'
If you want use REGX use -iname
如果你想使用 REGX 使用-iname
# find . -type f -iname 'text_for_search'
for file search用于文件搜索find / -xdev -name settings.xml
--> whole computer find / -xdev -name settings.xml
--> 整台计算机find ./ -xdev -name settings.xml
--> current directory & its sub directory find ./ -xdev -name settings.xml
--> 当前目录及其子目录
for files with extension type对于具有扩展名类型的文件
find . -type f -name "*.iso"
I am surprised to see that locate is not used heavily when we are to go recursively.我很惊讶地发现,当我们要递归时,locate 并没有被大量使用。
I would first do a locate "$PWD" to get the list of files in the current folder of interest, and then run greps on them as I please.我会先定位“$PWD”以获取当前感兴趣的文件夹中的文件列表,然后随意对它们运行 greps。
locate "$PWD" | grep -P <pattern>
Of course, this is assuming that the updatedb is done and the index is updated periodically.当然,这是假设 updatedb 已经完成并且索引会定期更新。 This is much faster way to find files than to run a find and asking it go down the tree.这种查找文件的方法比运行查找并要求它沿树向下查找要快得多。 Mentioning this for completeness.为了完整起见,提到这一点。 Nothing against using find, if the tree is not very heavy.如果树不是很重,没有什么反对使用 find 的。
Default way to search for recursive file, and available in most cases is搜索递归文件的默认方式,在大多数情况下可用
find . -name "filepattern"
It starts recursive traversing for filename or pattern from within current directory where you are positioned.它从您所在的当前目录开始递归遍历文件名或模式。 With find command, you can use wildcards, and various switches, to see full list of options, type使用 find 命令,您可以使用通配符和各种开关来查看完整的选项列表,输入
man find
or if man pages aren't available at your system或者如果您的系统上没有手册页
find --help
However, there are more modern and faster tools then find, which are traversing your whole filesystem and indexing your files, one such common tool is locate or slocate/mlocate, you should check manual of your OS on how to install it, and once it's installed it needs to initiate database, if install script don't do it for you, it can be done manually by typing然而,有更多现代和更快的工具然后找到,它们遍历你的整个文件系统并索引你的文件,一个这样的常用工具是定位或定位/mlocate,你应该检查你的操作系统手册关于如何安装它,一旦它安装它需要启动数据库,如果安装脚本不为你做,可以通过键入手动完成
sudo updatedb
And, to use it to look for some particular file type并且,使用它来查找某些特定的文件类型
locate filename
Or, to look for filename or patter from within current directory, you can type:或者,要从当前目录中查找文件名或模式,您可以键入:
pwd | xargs -n 1 -I {} locate "filepattern"
It will look through its database of files and quickly print out path names that match pattern that you have typed.它将查看其文件数据库并快速打印出与您键入的模式匹配的路径名。 To see full list of locate's options, type: locate --help
or man locate
要查看 locate 选项的完整列表,请输入: locate --help
或man locate
Additionally you can configure locate to update it's database on scheduled times via cron job, so sample cron which updates db at 1AM would look like:此外,您可以配置 locate 以通过 cron 作业在预定时间更新其数据库,因此在凌晨 1 点更新 db 的示例 cron 如下所示:
0 1 * * * updatedb
These cron jobs need to be configured by root, since updatedb needs root privilege to traverse whole filesystem.这些 cron 作业需要由 root 配置,因为 updatedb 需要 root 权限才能遍历整个文件系统。
以下命令将列出当前及其子文件夹中具有确切名称“模式”(例如)的所有文件。
find ./ -name "pattern"
If you want to search special file with wildcard, you can used following code:如果要使用通配符搜索特殊文件,可以使用以下代码:
find . -type f -name "*.conf"
Suppose, you want to search every .conf files from here:假设,您想从这里搜索每个 .conf 文件:
.
means search started from here (current place)表示从此处开始搜索(当前位置)
-type
means type of search item that here is file (f). -type
表示搜索项的类型,这里是文件 (f)。
-name
means you want to search files with *.conf names. -name
表示您要搜索具有*.conf名称的文件。
Below command helps to search for any files下面的命令有助于搜索任何文件
1) Irrespective of case 1) 不分情况
2) Result Excluding folders without permission 2)结果未经许可排除文件夹
3) Searching from the root or from the path you like. 3)从根目录或您喜欢的路径搜索。 Change / with the path you prefer.使用您喜欢的路径更改 / 。
Syntax :句法 :
find -iname '' 2>&1 |查找 -iname '' 2>&1 | grep -v "Permission denied" grep -v "权限被拒绝"
Example例子
find / -iname 'C*.xml' 2>&1 |查找 / -iname 'C*.xml' 2>&1 | grep -v "Permission denied" grep -v "权限被拒绝"
find / -iname '*C*.xml' 2>&1 | grep -v "Permission denied"
这将搜索当前和子目录中的所有相关文件,分别计算它们的行数以及完全计算它们的行数:
find . -name "*.wanted" | xargs wc -l
Try with fd
command if installed.如果已安装,请尝试使用fd
命令。 Install instruction安装说明
find all file starts with 'name'查找所有以“名称”开头的文件
fd "name*"
This command ignores all .hidden
and .gitignore
ed files.此命令忽略所有.hidden
和.gitignore
ed 文件。
To include .gitignore
ed files, add -I
option as below要包含.gitignore
ed 文件,请添加-I
选项,如下所示
fd -I "name*"
To include hidden files, add -H
option as below要包含隐藏文件,请添加-H
选项,如下所示
fd -H "name*"
You can also use ripgrep.您也可以使用 ripgrep。 Man page: https://www.mankier.com/1/rg#--files手册页: https://www.mankier.com/1/rg#--files
example:例子:
Seach all the filenames contain substring psql搜索所有包含 substring psql的文件名
rg --files | rg psql
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.