问题描述
我想搜索一个可能包含 kavi 或 kabhi 的文件名.我在终端上写了命令:
I want to search a filename which may contain kavi or kabhi.I wrote command in the terminal:
ls -l *ka[vbh]i*
在 ka 和 i 之间,可能存在 v 或 bh .
Between ka and i there may be v or bh .
我编写的代码不正确.正确的命令是什么?
The code I wrote isn't correct. What would be the correct command?
推荐答案
执行此操作的一种好方法是使用扩展的glob.有了它们,您可以在Bash上执行正则表达式.
A nice way to do this is to use extended globs. With them, you can perform regular expressions on Bash.
要启动,您必须启用extglob
功能,因为默认情况下它已禁用:
To start you have to enable the extglob
feature, since it is disabled by default:
shopt -s extglob
然后,编写具有以下条件的正则表达式:stuff + ka
+ v
或bh
+ i
+ stuff.在一起:
Then, write a regex with the required condition: stuff + ka
+ either v
or bh
+ i
+ stuff. All together:
ls -l *ka@(v|bh)i*
语法与常规正则表达式略有不同,因此您需要阅读扩展的globs 那...
The syntax is a bit different from the normal regular expressions, so you need to read in Extended Globs that...
测试
$ ls
a.php AABB AAkabhiBB AAkabiBB AAkaviBB s.sh
$ ls *ka@(v|bh)i*
AAkabhiBB AAkaviBB
这篇关于Linux终端中的文件全局模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!