简体   繁体   English

如何根据通配符匹配递归查找当前文件夹和子文件夹中的所有文件?

[英]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?如何根据通配符匹配递归查找当前文件夹和子文件夹中的所有文件?

Use find :使用find

find . -name "foo*"

find needs a starting point, so the . find需要一个起点,所以. (dot) points to the current directory. (dot) 指向当前目录。

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解释

  1. The first parameter is the directory you want to search.第一个参数是要搜索的目录。
  2. By default find does recursion.默认情况下find进行递归。
  3. The -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
  4. The quotes around the wildcard pattern are required.通配符模式周围的引号是必需的。
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 --helpman 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

With Python>3.5, using glob , .使用 Python>3.5,使用glob. pointing to your current folder and looking for .txt files:指向您当前的文件夹并查找.txt文件:

 python -c "import glob;[print(x) for x in glob.glob('./**/*txt', recursive=True)]"

For older versions of Python, you can install glob2对于旧版本的 Python,您可以安装glob2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何基于正则表达式递归查找当前文件夹和子文件夹中的所有文件 - How can I recursively find all files in current and subfolders based on regular expressions 如何递归地将文件夹及其所有子文件夹和文件设置为 drwxrwxrwx - How do I recursively set a folder and all of its subfolders and files to drwxrwxrwx 如何删除//当前文件夹和子文件夹中所有文件中的注释行? - How to delete // comment lines in all files in current folder and subfolders? 在文件夹和子文件夹中查找所有文件的另一种方法 - Alternative way to find all files in a folder and subfolders 如何使 shell 脚本递归扫描目录并根据名称模式将某些文件添加到 Git? - How can I make shell script to recursively scan a directory and add certain files based on their name patterns to Git? 如何以文件夹及其子文件夹的形式递归复制文件作为符号链接 - How to recursively copy files under a folder and its subfolders as symbolic links Linux:如何以相对于当前路径的路径递归列出所有文件,并且不包括当前路径 - Linux: How to list all files recursively with path relative to the current path, and do not include current path 如何在与命令行中的另一个模式匹配的目录中“查找”匹配模式的文件? - How can I `find` files matching pattern within a directory matching another pattern from the command line? 递归查找文件并根据其完整路径重命名 - Find files recursively and rename based on their full path 如何递归卷曲所有文件 - How to curl all files recursively
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM