List files not matching a pattern?

前端 未结 9 765
我在风中等你
我在风中等你 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 09:11

    With an appropriate version of find, you could do something like this, but it's a little overkill:

    find . -maxdepth 1 ! -name '*.jar'
    

    find finds files. The . argument specifies you want to start searching from ., i.e. the current directory. -maxdepth 1 tells it you only want to search one level deep, i.e. the current directory. ! -name '*.jar' looks for all files that don't match the regex *.jar.

    Like I said, it's a little overkill for this application, but if you remove the -maxdepth 1, you can then recursively search for all non-jar files or what have you easily.

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

    Use egrep-style extended pattern matching.

    ls !(*.jar)
    

    This is available starting with bash-2.02-alpha1. Must first be enabled with

    shopt -s extglob
    

    As of bash-4.1-alpha there is a config option to enable this by default.

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

    POSIX defines non-matching bracket expressions, so we can let the shell expand the file names for us.

    ls *[!j][!a][!r]
    

    This has some quirks though, but at least it is compatible with about any unix shell.

    0 讨论(0)
提交回复
热议问题