如何在linux shell脚本中使用正则表达式搜索文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5791602/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:46:47  来源:igfitidea点击:

How search for files using regex in linux shell script

regexlinuxshellsearch

提问by Mahakaal

Suppose, I want to search for files having python in filename in it, in all sub directories of linux, from a shell script. How can I search in all locations using regex?

假设,我想从 shell 脚本在 linux 的所有子目录中搜索文件名中包含 python 的文件。如何使用正则表达式搜索所有位置?

采纳答案by John Kugelman

Find all .py files.

查找所有 .py 文件。

find / -name '*.py'

Find files with the word "python" in the name.

查找名称中带有“python”一词的文件。

find / -name '*python*'

Same as above but case-insensitive.

同上,但不区分大小写。

find / -iname '*python*'

Regex match, more flexible. Find both .py files and files with the word "python" in the name.

正则匹配,更灵活。查找 .py 文件和名称中带有“python”一词的文件。

find / -regex '.*python.*\|.*\.py'

回答by Crayon Violent

find / -name "python"

[filler text to meet min.]

[满足分钟的填充文本]

回答by Malcolm Box

If simple shell regexps will suffice, then you can use find

如果简单的 shell regexp 就足够了,那么您可以使用 find

find /linux -name "*python*"

Otherwise, I'd say use ack (http://betterthangrep.com/)

否则,我会说使用 ack (http://betterthangrep.com/)