bash - 如何使用 Bash 回显包含匹配文件的目录?

标签 bash shell command-line find grep

我想编写一个 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/

相关文章:

运行少量脚本的 linux 从中央脚本启动

python - 来自 python os.system `RTNETLINK answers: No such file or directory` 的错误答案

node.js - 使用node和npm qrcode包生成批量二维码

bash - 运行命令直到返回代码大于 x

python - 使用 subprocess.call 从 Python 中调用 awk 时出现问题

linux - 在Linux中,日志输出产生后如何保存到文件中?

bash - 为什么 docker-compose exec 仅适用于某些服务?

string - 两个字符串之间的 ASCII 差异

linux - 执行rm前提示文件列表

windows - 从命令行运行 R 代码 (Windows)