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

Linux 在目录模式中查找文件的Shell命令,linux,bash,shell,console,Linux,Bash,Shell,Console,使用shell命令,我需要以以下目录模式列出服务器上的所有文件: /home/*/public_html/images/*.php 有几个问题需要很长时间才能手动完成。我真的不知道什么时候会用到这些命令。使用PHP函数 它将返回匹配路径字符串的数组。您也可以只使用: ls /home/*/public_html/images/*.php 或: 从shell中。让bash为您展开文件,并使用ls列出它们: ls /home/*/public_html/images/*.php 示例输出: /h

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

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

有几个问题需要很长时间才能手动完成。我真的不知道什么时候会用到这些命令。

使用PHP函数

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

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


从shell中。

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

ls /home/*/public_html/images/*.php
示例输出:

/home/grant/public_html/images/bar.php
/home/grant/public_html/images/foo.php
/home/marcog/public_html/images/helloworld.php
Shell脚本:

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

然后可以更改-exec命令,对返回的文件执行任何操作。在本例中,我们将响应它们,但您也可以轻松地执行其他操作。

我相信他是在shell中执行此操作的,而不是在php中。@marcog,它被标记为php。但这并不重要。PHP标签可能只是来自于他正在列出.PHP文件的事实。从技术上讲,您可以将PHP用作shell脚本,尽管这感觉很尴尬。;)对这是正确的语法。它不起作用吗?还是你想让它更快?如果是这样,就不可能有内在的优化。只是选择和解决办法
定位| grep
作为搜索缓存目录列表的工具。这将匹配
/home/foo/public\u html/images/file.php
,但不匹配
/home/foo/bar/public\u html/images/file.php
。我可以在任何深度匹配目录吗?如下面的另一个答案所述,使用
-path
可以在任何深度匹配目录。(例如,
-path“*/images/*”将匹配具有
images`目录的任何路径中的任何文件。)谢谢,这是我需要的。它在
/path/to/directory/foo/match/this/path
/path/to/directory/foo/bar/match/this/path/baz/
find /path/to/directory/.  -path "*/match/this/path/*" -type f -name "*.php"
find /home/*/public_html/images -iname "*php" -exec echo {} \;
find /path/to/directory/.  -path "*/match/this/path/*" -type f -name "*.php"