Regular Expression usage with ls

前端 未结 2 1445
故里飘歌
故里飘歌 2020-11-30 05:45

I am trying to use ER (Extended Regular Expressions) with ls like ls .+\\..+.

I am trying to print all files which contains an ext

相关标签:
2条回答
  • 2020-11-30 06:08

    You are confusing regular expression with shell globbing. If you want to use regular expression to match file names you could do:

    $ ls | egrep '.+\..+'
    
    0 讨论(0)
  • 2020-11-30 06:16

    You don't say what shell you are using, but they generally don't support regular expressions that way, although there are common *nix CLI tools (grep, sed, etc) that do.

    What shells like bash do support is globbing, which uses some similiar characters (eg, *) but is not the same thing.

    Newer versions of bash do have a regular expression operator, =~:

    for x in `ls`; do 
        if [[ $x =~ .+\..* ]]; then 
            echo $x; 
        fi; 
    done
    
    0 讨论(0)
提交回复
热议问题