如何在具有多个条件的bash shell中使用find语句

时间:2019-04-13 23:30:48

标签: bash shell

我需要显示“最终”目录和子目录中的所有文件 以2下划线开头的目录,后跟任意数量的目录 字符,以数字结尾,扩展名为“ .c”。

我可以在这里得到一些帮助吗?我无法弄清楚如何使用满足条件的多个条件。

#!/bin/bash
fine . -type f -iname "__*"
find . -type f -iname "*.c"

2 个答案:

答案 0 :(得分:1)

如果我正确理解了所需的文件名模式,则应该可以:

find . -type f -iname "__*[0-9].c"

这几乎是您的描述到glob模式语法的字面翻译:

"__" (start with 2 underscore)
"*" (any number of any characters)
"[0-9]" (a number)
".c" (with the “.c” extension)

唯一与您的描述不符的是,它只查找“ .c”之前的一位数字,而不是潜在的多位数字。但是,如果有一个以上的数字,则“ *”将匹配除最后一位以外的所有数字,所以这不是问题。

答案 1 :(得分:0)

如果任一条件为真:

find . -type f -iname '__*' -o -iname '*.c'

如果都必须为真,则使用-a代替-o

find . -type f -iname '__*' -a -iname '*.c'

当然,在这种情况下,这样做会简单得多

find . -type f -iname '__*.c'