我想编写一个 bash 脚本,它将使用包含特定文件的所有目录的列表。我可以使用 find
来回显每个匹配文件的路径。我只想列出包含至少一个匹配文件的目录的路径。
例如,给定以下目录结构:
dir1/
matches1
matches2
dir2/
no-match
命令(寻找'matches*'
)只会输出到dir1
的路径。
作为额外的背景,我使用它来查找包含 Java .class 文件的每个目录。
最佳答案
find . -name '*.class' -printf '%h\n' | sort -u
来自 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"."
.
关于bash - 如何使用 Bash 回显包含匹配文件的目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2282686/