bash 文件名通配 - 对以大写字母开头的文件进行操作

发布于 2024-09-19 09:21:25 字数 659 浏览 3 评论 0原文

假设我有一个包含以下 jpeg 文件的文件夹:

adfjhu.jpg  Afgjo.jpg  
Bdfji.jpg   bkdfjhru.jpg
Cdfgj.jpg   cfgir.jpg
Ddfgjr.jpg  dfgjrr.jpg

如何删除或列出以大写字母开头的文件?

这可以通过结合使用 findgrepxargs 来解决。

但是 bash 中的正常文件匹配/模式匹配是可能的吗?

下面的 cmd 不起作用,因为(据我所知)LANG 设置为 en_US 和整理顺序。

$ ls [A-Z]*.jpg
Afgjo.jpg  Bdfji.jpg  bkdfjhru.jpg  Cdfgj.jpg  cfgir.jpg  Ddfgjr.jpg  dfgjrr.jpg

这种方法可行

$ ls +(A|B|C|D)*.jpg
Afgjo.jpg  Bdfji.jpg  Cdfgj.jpg  Ddfgjr.jpg

,但我不想为所有角色 AZ 执行此操作以获得通用解决方案!

那么这可能吗?

干杯 //弗雷德里克

Lets say I have a folder with the following jpeg-files:

adfjhu.jpg  Afgjo.jpg  
Bdfji.jpg   bkdfjhru.jpg
Cdfgj.jpg   cfgir.jpg
Ddfgjr.jpg  dfgjrr.jpg

How do I remove or list the files that starts with a capital?

This can be solved with a combination of find, grep and xargs.

But it is possible with normal file-globbing/pattern matching in bash?

cmd below doesn't work due to the fact that (as far as I can tell) LANG is set to en_US
and the collation order.

$ ls [A-Z]*.jpg
Afgjo.jpg  Bdfji.jpg  bkdfjhru.jpg  Cdfgj.jpg  cfgir.jpg  Ddfgjr.jpg  dfgjrr.jpg

This sort of works

$ ls +(A|B|C|D)*.jpg
Afgjo.jpg  Bdfji.jpg  Cdfgj.jpg  Ddfgjr.jpg

But I don't wanna do this for all characters A-Z for a general solution!

So is this possible?

cheers
//Fredrik

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

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

发布评论

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

评论(3

对你再特殊 2024-09-26 09:21:26

您应该将区域设置设置为 C(或 POSIX)区域设置。

$ LC_ALL=C ls [A-Z]*.jpg

$ LC_ALL=C ls [[:upper:]]*.jpg

阅读此处了解更多信息: http://www.opengroup.org/onlinepubs/ 007908799/xbd/locale.html

you should set your locale to the C (or POSIX) locale.

$ LC_ALL=C ls [A-Z]*.jpg

or

$ LC_ALL=C ls [[:upper:]]*.jpg

read here for more information: http://www.opengroup.org/onlinepubs/007908799/xbd/locale.html

氛圍 2024-09-26 09:21:26

将括号表达式与字符类一起使用:

ls -l [[:upper:]]*

请参阅 man 7 regex 获取字符类列表和其他信息。

从该页面:

在方括号表达式中,“[:”和“:]”括起来的字符类名称代表属于该类的所有字符的列表。标准字符类名称是:

alnum 数字标点符号  
阿尔法图空间  
空白 下 上  
ctrl 打印 x 数字  

Use a bracket expression with a character class:

ls -l [[:upper:]]*

See man 7 regex for a list of character classes and other information.

From that page:

Within a bracket expression, the name of a character class enclosed in '[:' and ':]' stands for the list of all characters belonging to that class. Standard character class names are:

alnum    digit    punct  
alpha    graph    space  
blank    lower    upper  
cntrl    print    xdigit  
孤独难免 2024-09-26 09:21:26

使用 grep

ls | grep -e ^[A-Z]

如果你想使用 for 循环:

for i in $(ls | grep -e ^[A-Z]); do echo $i ;done 

Use grep:

ls | grep -e ^[A-Z]

If you want make more use a for loop:

for i in $(ls | grep -e ^[A-Z]); do echo $i ;done 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文