在 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/