Bash globbing - 在一些特定情况下自动扩展?
我知道通配符 *
(单独)将以这样的方式扩展,即“当前文件夹中的所有非隐藏文件”,其中隐藏文件是以句点为前缀的文件。
我认为有两个用例很有用,但我不知道如何正确执行:
How can you glob for...“当前文件夹中的所有文件,包括隐藏文件,但不包括 < code>. 或
..
"?如何查找...“当前文件夹中的所有隐藏文件(且仅隐藏文件),但不包括
.
或..
”?< /p>
I understand that the wildcard *
(by itself) will expand in such a way that it means "all non-hidden files in the current folder" with hidden files being those prefixed by a period.
There are two use cases that I would think are useful, but I don't know how to properly do:
How can you glob for... "All files in the current folder, including hidden files, but not including
.
or..
"?How can you glob for... "All hidden files (and only hidden files) in the current folder, but not including
.
or..
"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要扩展 paviums 答案并回答您问题的第二部分,除
.
和..
之外的所有文件都可以这样指定:根据您的具体用例,最好设置
dotglob
shell 选项,以便 bash 默认情况下在*
扩展中包含点文件:To expand on paviums answer and answer the second part of your question, all files except
.
and..
could be specified like this:Depending on your exact use case it might be better to set the
dotglob
shell option, so that bash includes dotfiles in expansions of*
by default:Bash Cookbook 建议满足您的第二个要求的解决方案。
作为指定“点文件”但避免
.
和..
当然,
ls
有-A
> 选项,但这不是通配符。The Bash Cookbook suggests a solution to your 2nd requirement.
as a way of specifying 'dot files' but avoiding
.
and..
Of course,
ls
has the-A
option, but that's not globbing.结合 sth 和 pavium 答案
Combining sth and pavium answers
为了满足你的第一个案例:
或
编辑:
这个似乎得到了一切,但比ephemient短
To meet your first case:
or
Edit:
This one seems to get everything, but is shorter than ephemient's
“所有文件”和“所有隐藏文件”是指仅文件,还是指文件和目录?通配符对名称进行操作,而不管它属于文件还是目录。其他人对于使用通配查找隐藏名称和非隐藏名称给出了很好的答案,但您可能需要使用
find
命令作为可以区分类型的更简单的替代方案。查找“当前文件夹中的所有文件,包括隐藏文件,但不包括.或..”:
查找“当前文件夹中的所有文件和目录,包括隐藏文件,但不包括.或..”:
查找“当前文件夹中的所有隐藏文件(且仅隐藏文件),但不包括 . 或 ..”:
查找“当前文件夹中的所有隐藏文件和目录(且仅隐藏文件和目录),但不包括 . 或..":
请注意,默认情况下 find 也会递归子目录,因此如果您想将其限制为仅当前目录,您可以使用:
By "all files" and "all hidden files" do you mean files-only, or do you mean both files and directories? Globbing operates on names irrespective of it belonging to a file or a directory. The other folks give good answers for using globbing to find hidden vs non-hidden names, but you may want to turn to the
find
command as an easier alternative that can distinguish between the types.To find "All files in the current folder, including hidden files, but not including . or ..":
To find "All files and directories in the current folder, including hidden files, but not including . or ..":
To find "All hidden files (and only hidden files) in the current folder, but not including . or ..":
To find "All hidden files and directories (and only hidden files and directories) in the current folder, but not including . or ..":
Note that by default find will recurse through subdirectories, too, so if you want to limit it to only the current directory you can use:
因此,尽管这已经很旧了 - 没有使用 shopt,但这似乎还没有得到完全解答。但是,扩展到目前为止作为答案给出的内容,这些对我有用:
1:
{*,.[!.]*,..?*}
2:
{.[ !.]*,..?*}
So, even though this is old - without using shopt, this doesn't seem to have been answered fully. But, expanding on what has been given as answers so far, these work for me:
1:
{*,.[!.]*,..?*}
2:
{.[!.]*,..?*}