你可以使用find
像这样:
find -type d -exec sh -c '[ -f "$0"/index.php ] && [ -f "$0"/style.css ]' '{}' \; -print
要搜索更多文件,只需添加它们即可&& [ -f "$0"/other_file ]
。返回码为sh
将指示是否可以找到所有文件。仅在以下情况下才会打印目录名称sh
已成功退出,即找到所有文件后。
测试一下:
$ mkdir dir1
$ touch dir1/a
$ mkdir dir2
$ touch dir2/a
$ touch dir2/b
$ find -type d -exec sh -c '[ -f "$0"/a ] && [ -f "$0"/b ]' '{}' \; -print
./dir2
这里我创建了两个目录,dir1
and dir2
. dir2
包含两个文件,因此会打印其名称。
正如 gniourf_gniourf 在评论中提到的(谢谢),没有必要使用sh
去做这个。相反,您可以这样做:
find -type d -exec test -f '{}'/a -a -f '{}'/b \; -print
[
and test
做同样的事。这种方法使用-a
代替&&
组合多个单独的测试,这减少了正在执行的进程数量。
为了响应您的评论,您可以将找到的所有目录添加到存档中,如下所示:
find -type d -exec test -f '{}'/a -a -f '{}'/b \; -print0 | tar --null -T - -cf archive.tar.bz2
The -print0
选项打印每个目录的名称,以空字节分隔。这很有用,因为它可以防止名称中包含空格的文件出现问题。名称由以下人员读取tar
并添加到 bzip 压缩的存档中。请注意,某些版本find
不支持-print0
选项。如果您的版本不支持,您可以使用-print
(并删除--null
选项tar
),具体取决于您的目录名称。