Bash globbing - 在一些特定情况下自动扩展?

发布于 2024-08-08 08:12:23 字数 321 浏览 10 评论 0原文

我知道通配符 * (单独)将以这样的方式扩展,即“当前文件夹中的所有非隐藏文件”,其中隐藏文件是以句点为前缀的文件。

我认为有两个用例很有用,但我不知道如何正确执行:

  1. How can you glob for...“当前文件夹中的所有文件,包括隐藏文件,但不包括 < code>. 或 .."?

  2. 如何查找...“当前文件夹中的所有隐藏文件(且仅隐藏文件),但不包括 ...”?< /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:

  1. How can you glob for... "All files in the current folder, including hidden files, but not including . or .."?

  2. How can you glob for... "All hidden files (and only hidden files) in the current folder, but not including . or .."?

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

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

发布评论

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

评论(6

π浅易 2024-08-15 08:12:24

要扩展 paviums 答案并回答您问题的第二部分,除 ... 之外的所有文件都可以这样指定:

{.[!.]*,*}

根据您的具体用例,最好设置 dotglob shell 选项,以便 bash 默认情况下在 * 扩展中包含点文件:

$ shopt -s dotglob
$ echo *
.tst

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:

$ shopt -s dotglob
$ echo *
.tst
も让我眼熟你 2024-08-15 08:12:24

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.

这样的小城市 2024-08-15 08:12:24

结合 sth 和 pavium 答案

# dot files but avoiding . and ..
.[!.]*

# all files but avoiding . and .. 
{.[!.]*,*}

Combining sth and pavium answers

# dot files but avoiding . and ..
.[!.]*

# all files but avoiding . and .. 
{.[!.]*,*}
北恋 2024-08-15 08:12:24

为了满足你的第一个案例:

echo {.,}[^.]*

echo {.,}[!.]*

编辑

这个似乎得到了一切,但比ephemient

echo {.*,}[^.]*

To meet your first case:

echo {.,}[^.]*

or

echo {.,}[!.]*

Edit:

This one seems to get everything, but is shorter than ephemient's

echo {.*,}[^.]*
菊凝晚露 2024-08-15 08:12:24

“所有文件”和“所有隐藏文件”是指仅文件,还是指文件和目录?通配符对名称进行操作,而不管它属于文件还是目录。其他人对于使用通配查找隐藏名称和非隐藏名称给出了很好的答案,但您可能需要使用 find 命令作为可以区分类型的更简单的替代方案。

查找“当前文件夹中的所有文件,包括隐藏文件,但不包括.或..”:

find . -type f

查找“当前文件夹中的所有文件和目录,包括隐藏文件,但不包括.或..”:

find . ! -name .

查找“当前文件夹中的所有隐藏文件(且仅隐藏文件),但不包括 . 或 ..”:

find . -name '.*' -type f

查找“当前文件夹中的所有隐藏文件和目录(且仅隐藏文件和目录),但不包括 . 或..":

find . -name '.*' ! -name .

请注意,默认情况下 find 也会递归子目录,因此如果您想将其限制为仅当前目录,您可以使用:

find . -maxdepth 1 -type f

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 ..":

find . -type f

To find "All files and directories in the current folder, including hidden files, but not including . or ..":

find . ! -name .

To find "All hidden files (and only hidden files) in the current folder, but not including . or ..":

find . -name '.*' -type f

To find "All hidden files and directories (and only hidden files and directories) in the current folder, but not including . or ..":

find . -name '.*' ! -name .

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:

find . -maxdepth 1 -type f
瑾兮 2024-08-15 08:12:24

因此,尽管这已经很旧了 - 没有使用 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:

{.[!.]*,..?*}

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