查找包含给定文件的目录? [英] Finding directories that contains given files?

查看:48
本文介绍了查找包含给定文件的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这是一个有趣的问题..我想找到一个包含所有给定文件的目录..到目前为止,我所做的工作如下

I hope this is an interesting question.. I want to find a directory that contains all the given files .. Until now what I have done is as follows

在Unix中查找多个文件...

Find multiple files in unix...

find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)

参考: http://alvinalexander.com/linux-unix/linux-find-multiple-filenames-patterns-command-example

仅查找包含给定文件的目录...

Finding only directories containing given files...

find . -type f -name '*.pdf' |sed 's#\(.*\)/.*#\1#' |sort -u

参考文献:我该如何执行一个命令,该命令将给我一个包含所有给定文件的目录...(这些文件必须位于给定目录中,而不能位于子目录..中,并且列表中给定的所有文件都必须存在)

How can I make a command that will give me a directory which contains all the given files... ( The files must be in given directory only not in sub directory .. and all the files given in a list must be present )

要搜索WordPress主题目录

Want to search for WordPress theme directories

推荐答案

您可以像这样使用 find :

find -type d -exec sh -c '[ -f "$0"/index.php ] && [ -f "$0"/style.css ]' '{}' \; -print

要搜索更多文件,只需将它们添加为&&[-f"$ 0"/other_file] . sh 的返回码将指示是否可以找到所有文件.只有在 sh 成功退出后,即找到所有文件后,才会打印目录名称.

To search for more files, simply add them like && [ -f "$0"/other_file ]. The return code of sh will indicate whether all the files could be found. The name of the directory will only be printed if sh has exited successfully, i.e. when all the files have been found.

对其进行测试:

$ 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 dir2 . dir2 包含两个文件,因此将其名称打印出来.

Here I've created two directories, dir1 and dir2. dir2 contains both files, so its name is printed.

正如gniourf_gniourf在评论中提到的(谢谢),没有必要使用 sh 来做到这一点.相反,您可以执行以下操作:

As gniourf_gniourf has mentioned in the comments (thanks), it is not necessary to use sh to do this. Instead, you can do this:

find -type d -exec test -f '{}'/a -a -f '{}'/b \; -print

[ test 做同样的事情.这种方法使用 -a 而不是& 来组合多个单独的测试,从而减少了正在执行的进程的数量.

[ and test do the same thing. This approach uses -a instead of && to combine multiple separate tests, which reduces the number of processes being executed.

为回应您的评论,您可以将找到的所有目录添加到存档中,如下所示:

In response to your comment, you can add all of the directories found to an archive like this:

find -type d -exec test -f '{}'/a -a -f '{}'/b \; -print0 | tar --null -T - -cf archive.tar.bz2

-print0 选项显示每个目录的名称,以空字节分隔.这很有用,因为它可以防止名称中包含空格的文件出现问题.名称由 tar 读取,并添加到bzip压缩的存档中.请注意,某些版本的 find 不支持 -print0 选项.如果您的版本不支持它,则可以使用 -print (并在 tar 中删除-null 选项),取决于您的目录名称.

The -print0 option prints the names of each of the directories, separated by a null byte. This is useful as it prevents problems with files containing spaces in their names. The names are read by tar and added to the bzip-compressed archive. Note that some versions of find do not support the -print0 option. If your version doesn't support it, you may be able to use -print (and remove the --null option to tar), depending on your directory names.

这篇关于查找包含给定文件的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆