使用 ls 进行通配以查找与特定模式匹配的所有文件

发布于 2025-01-04 05:15:14 字数 1020 浏览 4 评论 0原文

我试图在某个文件夹和任何子文件夹中找到所有 PDF 文件,只是在终端中,而不是在脚本中。我对 Linux 也很陌生,所以如果我错过了任何明显的事情,或者可能对诊断我的特定问题至关重要的事情,请道歉。

我正在使用 bash 4.1.5(1)-release (i486-pc-linux-gnu),并且在 google 上做了一些关于 glob 和 extglob 表达式的研究,看来我应该使用的语法是

$ ls **.pdf

This 然而发现什么都没有,因为当前文件夹 ./ 中没有与模式 *.pdf 匹配的文件;它似乎想要将 ** 读取为 *

ls: cannot access **.pdf: No such file or directory

其他地方有 PDF,在 1 到 5 深的子文件夹中(特别是在每个子文件夹中) 1-deep),其中一些我可以通过检查查看

$ ls */*.pdf

因此,ls 似乎工作正常。它的手册似乎不是很有帮助,因为我所看到的一切可能对我们使用 -R 标志调用 ls 有用,这并不能解决问题任何上述情况。

我尝试使用 extglob 模式(确保使用 shopt 打开它们)。我可以使用 ls */*?(.)pdf 查看深度 1 文件,但使用 ls .*(/*)pdf 看不到任何内容或ls .*(/*).pdf,甚至是在包含 PDF 的子目录中。

我在其他地方读过(参考 git 存储库中的 .gitignore 文件),** 模式并不适合所有人。

这会影响我吗?我该如何补救(最好没有超级用户权限)?这个(或一些相关问题)是否也会影响 extglob 功能?

I'm trying to locate all PDF files in some folder and any subfolder, just in the terminal, as opposed to in a script. I'm also quite new to linux, so apologise if I've missed anything obvious, or perhaps vital to diagnosing my particular problem.

I'm using bash 4.1.5(1)-release (i486-pc-linux-gnu), and have done some poking about on google about glob and extglob expressions, and it appears the syntax I should be using is

$ ls **.pdf

This however finds nothing, since there is no file matching the pattern *.pdf in the current folder ./; it appears to want to read ** as *:

ls: cannot access **.pdf: No such file or directory

There are PDFs elsewhere, in subfolders between 1 and 5 deep (in particular in every subfolder 1-deep), some of which I can see by checking with

$ ls */*.pdf

Hence, ls appears to be working properly. Its manual appears to not be very helpful, since all I could see that might be of any use us calling ls with the -R flag, which does not solve the problem in any of the above cases.

I tried using extglob patterns (making sure to turn them on with shopt). I can see my depth-1 files with ls */*?(.)pdf, but I can't see anything with ls .*(/*)pdf or ls .*(/*).pdf, even from within a subdirectory where there are PDFs.

I've read elsewhere (in reference to the .gitignore file in a git repository) that the ** pattern does not work for everyone.

Could this be affecting me, and how might I remedy it (ideally without superuser privileges)? Might this (or some related problem) be also affecting the extglob functionality?

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

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

发布评论

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

评论(3

榕城若虚 2025-01-11 05:15:14

您可能需要考虑 find

find . -name '*.pdf' -exec ls -l {} \;

find . -name '*.pdf' -ls

where 。是您当前的工作目录。 glob 功能随 4.0+ bash 一起提供。换句话说,glob 扩展是不可移植的。

You may want to consider find

find . -name '*.pdf' -exec ls -l {} \;

or

find . -name '*.pdf' -ls

where . is your current working directory. The glob functionality comes with 4.0+ bash. The glob extensions are not portable in other words.

荒路情人 2025-01-11 05:15:14

需要使用 extglobglobstar 选项才能从 glob 库获取扩展功能;它们是使用 shell 选项 (shopt) 实用程序打开的,如下所述。

要使用 globstar (这会导致 ** 模式的行为如 bash 手册中所述),必须使用 和 激活它(在 bash 4.0+ 中)以

shopt -s globstar

启用更多功能“功能”正则表达式类型表达式,如 ?(ab)*(ab) 其完整效果,请使用

shopt -s extglob

要再次关闭选项,请指定 -u 标志而不是s,例如

shopt -u globstar

The options extglob and globstar are required to get the extended functionality from the glob library; they are turned on using the shell options (shopt) utility as described below.

To use the globstar (which causes the ** pattern to behave as described in the bash manual) one must activate it (in bash 4.0+) with

shopt -s globstar

and to enable the more "functional" regex-type expressions like ?(ab) and *(ab) their full effect, use

shopt -s extglob

To turn the options off again, specify the -u flag instead of s, for eaxample

shopt -u globstar
伤痕我心 2025-01-11 05:15:14

也许 echo **/*.pdf 就是您想要的。

Maybe echo **/*.pdf is what you want.

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