zsh / bash上不区分大小写的Glob

时间:2022-09-01 23:38:22

I need to list all files whose names start with 'SomeLongString'. But the case of 'SomeLongString' can vary. How?

我需要列出名称以'SomeLongString'开头的所有文件。但'SomeLongString'的情况可能会有所不同。怎么样?

I am using zsh, but a bash solution is also welcome.

我正在使用zsh,但也欢迎使用bash解决方案。

4 个解决方案

#1


26  

ZSH:

ZSH:

$ unsetopt CASE_GLOB

Or, if you don't want to enable case-insensitive globbing in general, you can activate it for only the varying part:

或者,如果您不想一般地启用不区分大小写的通配符,则可以仅为不同的部分激活它:

$ print -l (#i)(somelongstring)*

This will match any file that starts with "somelongstring" (in any combination of lower/upper case). The case-insensitive flag applies for everything between the parentheses and can be used multiple times. Read the manual zshexpn(1) for more information.

这将匹配任何以“somelongstring”开头的文件(以大写/小写的任意组合)。不区分大小写的标志适用于括号之间的所有内容,并且可以多次使用。有关更多信息,请阅读手册zshexpn(1)。

UPDATE Almost forgot, you have to enable extendend globbing for this to work:

更新几乎忘了,你必须启用extendend globbing才能工作:

setopt extendedglob

#2


27  

bash:

庆典:

shopt -s nocaseglob

#3


11  

Depending on how deep you want to have this listing, find offers quite a lot in this regard:

根据您希望拥有此列表的深度,在这方面可以找到相当多的优惠:

find . -iname 'SomeLongString*' -maxdepth 1

This will only give you the files in the current directory. Important here is the -iname parameter instead of -name.

这只会为您提供当前目录中的文件。这里重要的是-iname参数而不是-name。

#4


1  


$ function i () {
> shopt -s nocaseglob; $*; shopt -u nocaseglob
> }
$ ls *jtweet*
ls: cannot access *jtweet*: No such file or directory
$ i ls *jtweet*
JTweet.pm  JTweet.pm~  JTweet2.pm  JTweet2.pm~

#1


26  

ZSH:

ZSH:

$ unsetopt CASE_GLOB

Or, if you don't want to enable case-insensitive globbing in general, you can activate it for only the varying part:

或者,如果您不想一般地启用不区分大小写的通配符,则可以仅为不同的部分激活它:

$ print -l (#i)(somelongstring)*

This will match any file that starts with "somelongstring" (in any combination of lower/upper case). The case-insensitive flag applies for everything between the parentheses and can be used multiple times. Read the manual zshexpn(1) for more information.

这将匹配任何以“somelongstring”开头的文件(以大写/小写的任意组合)。不区分大小写的标志适用于括号之间的所有内容,并且可以多次使用。有关更多信息,请阅读手册zshexpn(1)。

UPDATE Almost forgot, you have to enable extendend globbing for this to work:

更新几乎忘了,你必须启用extendend globbing才能工作:

setopt extendedglob

#2


27  

bash:

庆典:

shopt -s nocaseglob

#3


11  

Depending on how deep you want to have this listing, find offers quite a lot in this regard:

根据您希望拥有此列表的深度,在这方面可以找到相当多的优惠:

find . -iname 'SomeLongString*' -maxdepth 1

This will only give you the files in the current directory. Important here is the -iname parameter instead of -name.

这只会为您提供当前目录中的文件。这里重要的是-iname参数而不是-name。

#4


1  


$ function i () {
> shopt -s nocaseglob; $*; shopt -u nocaseglob
> }
$ ls *jtweet*
ls: cannot access *jtweet*: No such file or directory
$ i ls *jtweet*
JTweet.pm  JTweet.pm~  JTweet2.pm  JTweet2.pm~