是否可以制作一个匹配当前目录和所有子目录中文件的 glob?

发布于 2024-07-04 23:08:57 字数 354 浏览 7 评论 0原文

对于此目录结构:

.
|-- README.txt
|-- firstlevel.rb
`-- lib
    |-- models
    |   |-- foo
    |   |   `-- fourthlevel.rb
    |   `-- thirdlevel.rb
    `-- secondlevel.rb

3 directories, 5 files

glob 将匹配:

firstlevel.rb 
lib/secondlevel.rb 
lib/models/thirdlevel.rb
lib/models/foo/fourthlevel.rb

For this directory structure:

.
|-- README.txt
|-- firstlevel.rb
`-- lib
    |-- models
    |   |-- foo
    |   |   `-- fourthlevel.rb
    |   `-- thirdlevel.rb
    `-- secondlevel.rb

3 directories, 5 files

The glob would match:

firstlevel.rb 
lib/secondlevel.rb 
lib/models/thirdlevel.rb
lib/models/foo/fourthlevel.rb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兲鉂ぱ嘚淚 2024-07-11 23:08:57

看起来无法从 bash 完成,

如果您使用 zsh 则会

ls **/*.rb

产生正确的结果。

否则你可以劫持 ruby​​ 解释器(也可能是其他语言的解释器)

ruby -e "puts Dir.glob('**/*.rb')"

感谢 Chris 和 Gaius 的回答。

Looks like it can't be done from bash

If you using zsh then

ls **/*.rb

will produce the correct result.

Otherwise you can hijack the ruby interpreter (and probably those of other languages)

ruby -e "puts Dir.glob('**/*.rb')"

Thanks to Chris and Gaius for your answers.

北渚 2024-07-11 23:08:57

在 Ruby 本身中:

Dir.glob('**/*.rb') perhaps?

In Ruby itself:

Dir.glob('**/*.rb') perhaps?
那片花海 2024-07-11 23:08:57

在 zsh 中,**/*.rb 有效

In zsh, **/*.rb works

神经大条 2024-07-11 23:08:57

如果我错过了问题的真正要点,我深表歉意,但是,如果我使用 sh/bash/etc.,那么我可能会使用 find 来完成这项工作:

find . -name '*.rb' -type f

当在脚本中使用,find 更加灵活。

Apologies if I've missed the real point of the question but, if I was using sh/bash/etc., then I would probably use find to do the job:

find . -name '*.rb' -type f

Globs can get a bit nasty when used from within a script and find is much more flexible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文