如何呼应含猛砸匹配的文件目录? [英] How to echo directories containing matching file with Bash?
问题描述
我想写这将使用包含特定文件中的所有目录的列表bash脚本。我可以使用找到
相互呼应的路径和每一个匹配的文件。我只要列出的路径含有至少一个匹配的文件的目录。
例如,给出以下目录结构:
DIR1 /
matches1
matches2
DIR2 /
没有匹配
命令(寻找'匹配*'
)将只输出的路径, DIR1
。
作为额外的背景下,我使用这个发现其中包含Java .class文件的每个目录。
找到-name'的* .class-printf'%H \\ n'|排序-u
从找人
:
-printf
的格式的
%H
文件名的前导目录(除了最后元素)。如果文件名中没有斜杠(因为它是在当前目录)的%H
符扩展到,
。
块引用>I want to write a bash script which will use a list of all the directories containing specific files. I can use
find
to echo the path of each and every matching file. I only want to list the path to the directory containing at least one matching file.For example, given the following directory structure:
dir1/ matches1 matches2 dir2/ no-match
The command (looking for
'matches*'
) will only output the path todir1
.As extra background, I'm using this to find each directory which contains a Java .class file.
解决方案find -name '*.class' -printf '%h\n' | sort -u
From
man find
:
-printf
format
%h
Leading directories of file’s name (all but the last element). If the file name contains no slashes (since it is in the current directory) the%h
specifier expands to"."
.
这篇关于如何呼应含猛砸匹配的文件目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!