如何根据通配符匹配递归查找当前文件夹和子文件夹中的所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5905054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I recursively find all files in current and subfolders based on wildcard matching?
提问by john
How can I recursively find all files in current and subfolders based on wildcard matching?
如何根据通配符匹配递归查找当前文件夹和子文件夹中的所有文件?
采纳答案by tux21b
回答by IslandCow
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!
希望这可以帮助!
回答by Paul Whipp
Piping find into grep is often more convenient; it gives you the full power of regular expressions for arbitrary wildcard matching.
管道 find 到 grep 通常更方便;它为您提供了用于任意通配符匹配的正则表达式的全部功能。
For example, to find all files with case insensitive string "foo" in the filename:
例如,要查找文件名中不区分大小写字符串“foo”的所有文件:
~$ find . -print | grep -i foo
回答by kenorb
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 "**"; }
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
utilitywith a simple syntax, e.g. fd pattern
.
或者,您可以使用具有简单语法的fd
实用程序,例如fd pattern
.
回答by toddcscar
find -L . -name "foo*"
In a few cases, I have needed the -L parameter to handle symbolic directory links. 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. Using -L solves that issue. The symbolic link options for find are -P -L -H
在少数情况下,我需要 -L 参数来处理符号目录链接。默认情况下,符号链接被忽略。在这些情况下,这很令人困惑,因为我会将目录更改为子目录并查看与模式匹配的文件,但 find 不会返回文件名。使用 -L 解决了这个问题。find 的符号链接选项是 -P -L -H
回答by XYZ_Linux
find <directory_path> -type f -name "<wildcard-match>"
In the wildcard-match you can provide the string you wish to match e.g. *.c (for all c files)
在通配符匹配中,您可以提供您希望匹配的字符串,例如 *.c(对于所有 c 文件)
回答by Alberto
for file search
find / -xdev -name settings.xml
--> whole computer
find ./ -xdev -name settings.xml
--> current directory & its sub directory
for files with extension type
用于文件搜索
find / -xdev -name settings.xml
--> 整台计算机
find ./ -xdev -name settings.xml
--> 当前目录及其子目录,
用于查找具有扩展名类型的文件
find . -type f -name "*.iso"
回答by Aleksandar Pavi?
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
但是,有更多现代和更快的工具可以找到,它们遍历整个文件系统并为文件编制索引,其中一种常用工具是 locate 或 slocate/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 --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 权限才能遍历整个文件系统。
回答by user8848899
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'
回答by Reza Harasani
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).-name
means you want to search files with *.confnames.
.
表示从这里开始搜索(当前位置)-type
表示这里是文件(f)的搜索项的类型。-name
意味着您要搜索具有*.conf名称的文件。