简体   繁体   English

无论bash脚本中的大小写如何匹配文件扩展名

[英]How to match file extensions regardless of case in bash script

Let's just say i want to match the .abc extension but it could come over as .ABC or .AbC . 我只想说我想匹配.abc扩展名,但它可能会以.ABC.AbC How can I identify all of these variations of the .abc extension in order to process .abc files? 如何识别.abc扩展名的所有这些变体以处理.abc文件?

Right now i'm using: 现在我正在使用:

ls | grep -i .abc

but i've heard that piping to grep is usually not the best idea. 但是我听说grep的管道通常不是最好的主意。 Is there a better way to do this? 有一个更好的方法吗?

If you enter the extension literally, you can use character classes: 如果从字面上输入扩展名,则可以使用字符类:

ls *.[Aa][Bb][Cc]

You can also use the -iname option of find : 您还可以使用find-iname选项:

find -maxdepth 1 -iname '*.abc'

You can use the nocaseglob option to the shopt builtin to make globs not pay attention to case. 你可以使用nocaseglob选项购买内置的shopt ,使globs不注意大小写。

$ touch foo.abc foo.ABC
$ echo *.abc
foo.abc
$ shopt -s nocaseglob
$ echo *.abc
foo.ABC foo.abc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM