linux - 如何在 bash 中匹配 `find` 的文件 - 也在子目录中?

标签 linux bash find

在 linux bash 中,我有这样的目录:

.
├── index.md
├── rss.conf
└── tech
    └── comp.md

我正在尝试获取所有 *.md 文件的相对文件名列表。在这里查找一些答案,我收集了:find -name *.md

只输出./index.md

(奇怪的是,如果我在 cd ../ 之后运行命令,它会找到所有 *.md。)

我该如何解决这个问题?

最佳答案

find . -name *.md

只找到 index.md 因为命令实际上扩展为

find . -name index.md

也就是说,find 只能看到一个名字。 (这就是 glob 的工作原理——它们在命令执行之前在 shell 中展开。)

您需要做的只是将命令用引号引起来,这样 glob 就不会扩展。

find . -name '*.md'

一般来说,如果您想了解命令未按预期执行的原因,请在 shell 中运行 set -x。这将导致它在调用之前输出真正的命令。

$ set -x
+ set -x
$ find . -name *.md
+ find . -name index.md
./index.md
$ find . -name '*.md'
+ find . -name '*.md'
./index.md
./tech/comp.md

关于linux - 如何在 bash 中匹配 `find` 的文件 - 也在子目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42497699/

相关文章:

find - sequelize 从子表中查找父表

find - 在子目录上使用 find 并创建指向所有文件的符号链接(symbolic link)

linux - 如何修复 sed 无效命令代码'?

linux - linux下是否可以监控所有进程对文件系统的所有写访问

linux - 使用 O_TMPFILE 清理大页面......或其他方法?

regex - 使用基于 sed/awk 的 linux 替换字符串

linux - 安装设备不允许二进制文件

bash - 在 bash 脚本中间切换输出文件重定向

linux - 在 Bash 中循环文件的内容

linux - 对命令 'find' 找到的文件进行排序