如何列出每个带有特定扩展名的文件(带有bash的文件除外)

时间:2018-07-12 15:46:27

标签: bash windows-subsystem-for-linux

所以我有一个目录,其中包含多个不同类型的文件(例如pdf,txt等)。假设我要选择.example类型的每个文件,但我要如何选择呢?假设文件全称为ex_.example,其中_是介于0到30之间的数字。

我尝试了ls ex * [^ 12] *。example;除了1、2、11、12、21和22之外,我还有每个示例文件。我不知道如何获得除12号文件之外的每个文件。

谢谢!

2 个答案:

答案 0 :(得分:2)

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

  

!(pattern-list)

     

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

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

答案 1 :(得分:1)

我喜欢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开关进行控制。