Linux Shell命令在目录模式中查找文件

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

Shell command to find files in a directory pattern

linuxbashshellconsole

提问by Grant Unwin

With a shell command i need to list all files on my server in the following directory pattern:

使用 shell 命令,我需要以以下目录模式列出服务器上的所有文件:

/home/*/public_html/images/*.php

/home/*/public_html/images/*.php

Theres a few an its taking a long time to do this manually. I really have no idea when it comes to these commands.

手动执行此操作需要很长时间。当涉及到这些命令时,我真的不知道。

采纳答案by jmort253

Shell Script:

外壳脚本:

find /home/*/public_html/images -iname "*php" -exec echo {} \;

You can then change the -exec command to do whatever actions you want to the returned files. In this case, we echo them, but you could easily perform other actions as well.

然后,您可以更改 -exec 命令以对返回的文件执行任何操作。在这种情况下,我们会回应它们,但您也可以轻松地执行其他操作。

回答by Matthew Flaschen

Use the PHP globfunction

使用 PHP glob函数

glob('/home/*/public_html/images/*.php')

It will return an array of the matching path strings. You can also just use:

它将返回匹配路径字符串的数组。你也可以只使用:

ls /home/*/public_html/images/*.php

or:

或者:

for i in /tmp/*/public_html/images/*.php;
do 
some_command "$i"
done

from the shell.

从外壳。

回答by moinudin

Let bash expand the files for you and use lsto list them:

让 bash 为您展开文件并用于ls列出它们:

ls /home/*/public_html/images/*.php

Example output:

示例输出:

/home/grant/public_html/images/bar.php
/home/grant/public_html/images/foo.php
/home/marcog/public_html/images/helloworld.php

回答by karthik m

find /path/to/directory/.  -path "*/match/this/path/*" -type f -name "*.php"