当前目录中 file_root 后带有 2 个下划线的文件

files with 2 exact underscores after file_root in current directoy

我希望在 file_root 之后找到带有 2 个下划线的文件,如下所示:

file_root_stringA_stringB.txt

其中 stringA 和 stringB 都在 alnum 中 class

find /tmp -maxdepth 2 -type f -name "file_root_*_*.txt"

但我坚持:

file_root_aaa_bbb.txt # expected
file_root_aaa_bbb_c12.txt # not expecting

我怎样才能在 bash 中得到这个?谢谢

find /tmp -maxdepth 2 -type f -name "file_root_*_*.txt" -not -name "file_root_*_*_*.txt"

您可以使用正则表达式:

find /tmp -maxdepth 2 -type f -regextype posix-egrep -regex "^.*file_root_[^_]*_[^_]*\.txt"