List files not matching a pattern?

前端 未结 9 764
我在风中等你
我在风中等你 2020-11-28 08:30

Here\'s how one might list all files matching a pattern in bash:

ls *.jar

How to list the complement of a pattern? i.e. all files not match

相关标签:
9条回答
  • 2020-11-28 08:51

    Another approach can be using ls -I flag (Ignore-pattern).

    ls -I '*.jar'
    
    0 讨论(0)
  • 2020-11-28 08:59

    If your ls supports it (man ls) use the --hide=<PATTERN> option. In your case:

    $> ls --hide=*.jar
    

    No need to parse the output of ls (because it's very bad) and it scales to not showing multiple types of files. At some point I needed to see what non-source, non-object, non-libtool generated files were in a (cluttered) directory:

    $> ls src --hide=*.{lo,c,h,o}
    

    Worked like a charm.

    0 讨论(0)
  • 2020-11-28 08:59

    And if you want to exclude more than one file extension, separate them with a pipe |, like ls test/!(*.jar|*.bar). Let's try it:

    $ mkdir test
    $ touch test/1.jar test/1.bar test/1.foo
    $ ls test/!(*.jar|*.bar)
    test/1.foo
    

    Looking at the other answers you might need to shopt -s extglob first.

    0 讨论(0)
  • 2020-11-28 09:00

    One solution would be ls -1|grep -v '\.jar$'

    0 讨论(0)
  • 2020-11-28 09:02
    ls | grep -v '\.jar$'
    

    for instance.

    0 讨论(0)
  • 2020-11-28 09:04

    Little known bash expansion rule:

    ls !(*.jar)
    
    0 讨论(0)
提交回复
热议问题