bash 列出与模式不匹配的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8525437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:22:09  来源:igfitidea点击:

List files not matching a pattern?

bashwildcardglobls

提问by paislee

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

以下是在 bash 中列出与模式匹配的所有文件的方式:

ls *.jar

How to list the complement of a pattern? i.e. all files not matching *.jar?

如何列出模式的补码?即所有文件不匹配 *.jar?

采纳答案by Michael Krelin - hacker

ls | grep -v '\.jar$'

for instance.

例如。

回答by Christian

Use egrep-style extended pattern matching.

使用 egrep 风格的扩展模式匹配。

ls !(*.jar)

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

这从 bash-2.02-alpha1 开始可用。必须首先启用

shopt -s extglob

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

从 bash-4.1-alpha 开始,有一个配置选项可以默认启用它。

回答by ccarton

Little known bash expansion rule:

鲜为人知的 bash 扩展规则:

ls !(*.jar)

回答by stefanct

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

POSIX 定义了不匹配的括号表达式,因此我们可以让 shell 为我们扩展文件名。

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

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

虽然这有一些怪癖,但至少它与任何 unix shell 兼容。

回答by Dan Fego

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

使用适当版本的find,你可以做这样的事情,但这有点矫枉过正:

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

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

find查找文件。将.要开始搜索参数指定.,即当前目录。-maxdepth 1告诉它您只想搜索一级深度,即当前目录。! -name '*.jar'查找所有与 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.

就像我说的,对于这个应用程序来说有点矫枉过正,但是如果你删除了-maxdepth 1,你就可以递归地搜索所有非 jar 文件或者你有什么。

回答by fge

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

一种解决方案是 ls -1|grep -v '\.jar$'

回答by James Brown

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:

如果您想排除多个文件扩展名,请用管道分隔它们|,例如ls test/!(*.jar|*.bar). 让我们试试看:

$ 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 extglobfirst.

查看您可能需要shopt -s extglob首先查看的其他答案。

回答by haavee

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

如果您ls支持它 ( man ls),请使用该--hide=<PATTERN>选项。在你的情况下:

$> 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(因为它非常糟糕)的输出,并且可以扩展为不显示多种类型的文件。在某些时候,我需要查看(杂乱的)目录中包含哪些非源代码、非对象、非 libtool 生成的文件:

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

Worked like a charm.

像魅力一样工作。

回答by Divyanshu Srivastava

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

另一种方法可以使用ls -I标志(忽略模式)。

ls -I '*.jar'