如何列出每个具有特定扩展名的文件,但带有 bash 的文件除外

How can I list every file with a specific extension except one with bash

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

我试过 ls ex*[^12]*.example;我得到了除 1、2、11、12、21 和 22 之外的所有示例文件。我不明白如何获得除文件编号 12 之外的每个文件。

谢谢!

您可以使用扩展的 globbing。引用 Bash Reference Manual:

!(pattern-list)

Matches anything except one of the given patterns.

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

我喜欢 find 这个:

find . -name *.example # print every file with the .example extension seen from the current directory
find . -name *.example ! -name ex12.example # all but ex12.example

请注意,find 将递归搜索从指定目录开始的所有目录。如果需要,使用 -maxdepth 开关来控制它。