How can I recursively find all files in current and subfolders based on wildcard matching?
如何在基于通配符匹配的当前和子文件夹中递归地查找所有文件?
10 个解决方案
#1
1653
Use find for that:
使用找到的:
find . -name "foo*"
find
needs a starting point, and the .
(dot) points to the current directory.
寻找需要一个起点,和。(点)指向当前目录。
#2
152
Piping find into grep is often more convenient; 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:
例如,要在filename中查找带有大小写不敏感字符串“foo”的所有文件:
~$ find . -print | grep -i foo
#3
97
find
will find all files that match a pattern:
查找将找到匹配模式的所有文件:
find . -name "*foo"
However, if you want a picture:
然而,如果你想要一张照片:
tree -P "*foo"
Hope this helps!
希望这可以帮助!
#4
14
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解决了这个问题。找到的符号链接选项是-P -L -H。
#5
13
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)
在wildcard-match中,可以提供希望匹配的字符串。c(适用于所有c文件)
#6
13
If you shell supports a new globbing option (enable it 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:
我个人已经定义了这个壳函数:
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
#7
6
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'
#8
3
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
然而,有更多的现代和更快的工具,那么,遍历整个文件系统和索引文件,其中一个常见的工具是定位或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:
或者,从当前目录中查找文件名或patter,您可以输入:
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:
此外,您还可以通过cron作业来配置在预定时间更新它的数据库的位置,所以在1AM更新db的示例cron将是:
0 1 * * * updatedb
These cron jobs need to be configured by root, since updatedb needs root privilege to traverse whole filesystem.
这些cron作业需要由根来配置,因为updatedb需要root权限来遍历整个文件系统。
#9
2
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
用于文件搜索查找/ -xdev -名称设置。xml——>全电脑查找。/ -xdev -名称设置。xml——>当前目录及其子目录,用于扩展类型的文件。
find . -type f -name "*.iso"
#10
0
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 *.conf names.
。意思是搜索从这里开始(当前位置)-type表示搜索项的类型,这里是file (f). -name表示你想要搜索带有*的文件。配置名称。
#1
1653
Use find for that:
使用找到的:
find . -name "foo*"
find
needs a starting point, and the .
(dot) points to the current directory.
寻找需要一个起点,和。(点)指向当前目录。
#2
152
Piping find into grep is often more convenient; 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:
例如,要在filename中查找带有大小写不敏感字符串“foo”的所有文件:
~$ find . -print | grep -i foo
#3
97
find
will find all files that match a pattern:
查找将找到匹配模式的所有文件:
find . -name "*foo"
However, if you want a picture:
然而,如果你想要一张照片:
tree -P "*foo"
Hope this helps!
希望这可以帮助!
#4
14
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解决了这个问题。找到的符号链接选项是-P -L -H。
#5
13
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)
在wildcard-match中,可以提供希望匹配的字符串。c(适用于所有c文件)
#6
13
If you shell supports a new globbing option (enable it 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:
我个人已经定义了这个壳函数:
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
#7
6
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'
#8
3
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
然而,有更多的现代和更快的工具,那么,遍历整个文件系统和索引文件,其中一个常见的工具是定位或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:
或者,从当前目录中查找文件名或patter,您可以输入:
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:
此外,您还可以通过cron作业来配置在预定时间更新它的数据库的位置,所以在1AM更新db的示例cron将是:
0 1 * * * updatedb
These cron jobs need to be configured by root, since updatedb needs root privilege to traverse whole filesystem.
这些cron作业需要由根来配置,因为updatedb需要root权限来遍历整个文件系统。
#9
2
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
用于文件搜索查找/ -xdev -名称设置。xml——>全电脑查找。/ -xdev -名称设置。xml——>当前目录及其子目录,用于扩展类型的文件。
find . -type f -name "*.iso"
#10
0
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 *.conf names.
。意思是搜索从这里开始(当前位置)-type表示搜索项的类型,这里是file (f). -name表示你想要搜索带有*的文件。配置名称。