开发者

bash filename globbing - operate on files starting with capital

开发者 https://www.devze.com 2023-01-16 08:16 出处:网络
Lets say I have a folder with the 开发者_C百科following jpeg-files: adfjhu.jpgAfgjo.jpg Bdfji.jpgbkdfjhru.jpg

Lets say I have a folder with the 开发者_C百科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


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


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  


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 
0

精彩评论

暂无评论...
验证码 换一张
取 消