我想写一个bash脚本,它将使用包含特定文件的所有目录的列表。我可以使用find回显每个匹配文件的路径。我只想列出至少包含一个匹配文件的目录的路径。
例如,给定以下目录结构:
dir1/
matches1
matches2
dir2/
no-match该命令(查找'matches*')将仅输出dir1的路径。
作为额外的背景知识,我使用它来查找包含一个Java .class文件的每个目录。
发布于 2010-02-18 01:03:04
find . -name '*.class' -printf '%h\n' | sort -u来自man find
-printf格式
文件名的%h前导目录(最后一个元素除外)。如果文件名不包含斜杠(因为它在当前目录中),则%h说明符将扩展为".".
发布于 2013-12-15 23:31:16
在OS X和FreeBSD上,使用lacks the -printf option的find,这将起作用:
find . -name *.class -print0 | xargs -0 -n1 dirname | sort --uniquexargs中的-n1将每次调用dirname时从标准输入获取的最大参数数设置为1
发布于 2010-02-18 01:04:32
GNU查找
find /root_path -type f -iname "*.class" -printf "%h\n" | sort -uhttps://stackoverflow.com/questions/2282686
复制相似问题