File Glob Patterns in Linux terminal

后端 未结 3 2003
北海茫月
北海茫月 2021-01-25 05:08

I want to search a filename which may contain kavi or kabhi. I wrote command in the terminal:

ls -l *ka[vbh]i*
<
3条回答
  •  梦谈多话
    2021-01-25 05:30

    A nice way to do this is to use extended globs. With them, you can perform regular expressions on Bash.

    To start you have to enable the extglob feature, since it is disabled by default:

    shopt -s extglob
    

    Then, write a regex with the required condition: stuff + ka + either v or bh + i + stuff. All together:

    ls -l *ka@(v|bh)i*
    

    The syntax is a bit different from the normal regular expressions, so you need to read in Extended Globs that...

    @(list): Matches one of the given patterns.

    Test

    $ ls
    a.php  AABB  AAkabhiBB  AAkabiBB  AAkaviBB  s.sh
    $ ls *ka@(v|bh)i*
    AAkabhiBB  AAkaviBB
    

提交回复
热议问题