首页 > 解决方案 > 如何列出具有特定扩展名的每个文件,除了带有 bash 的文件

问题描述

所以我有一个目录,其中包含多个不同类型的文件(例如 pdf、txt 等)。假设我想选择 .example 类型的每个文件,除了一个我将如何去做?假设这些文件都称为 ex_.example,其中 _ 是 0 到 30 之间的数字。

我试过 ls ex*[^12]*.example; 我得到了除了 1、2、11、12、21 和 22 之外的每个示例文件。我不明白除了 JUST 文件号 12 之外,我怎么能得到每个文件。

谢谢!

标签: bashwindows-subsystem-for-linux

解决方案


您可以使用扩展通配符。引用Bash 参考手册

!(pattern-list)

匹配除给定模式之一之外的任何内容。

shopt -s extglob               # enables extended globbing
ls -l ex!(12).example          # for more numbers, you can use !(12|18|...)

推荐阅读