Bash 从shell中的通配符搜索中排除字符串
我试图从文件搜索中排除某个字符串 假设我有一个文件列表:file\u Michael.txt、file\u Thomas.txt、file\u Anne.txt 我想能够写一些像这样的东西Bash 从shell中的通配符搜索中排除字符串,bash,wildcard,Bash,Wildcard,我试图从文件搜索中排除某个字符串 假设我有一个文件列表:file\u Michael.txt、file\u Thomas.txt、file\u Anne.txt 我想能够写一些像这样的东西 ls *<and not Thomas>.txt 使用单个字符也很容易: ls *[^s].txt 但是如何用一根绳子来做呢 塞巴斯蒂安和巴什 shopt -s extglob ls !(*Thomas).txt 如果第一行表示“设置扩展全局”,请参阅以了解更多信息 其他一些方法可以是: f
ls *<and not Thomas>.txt
使用单个字符也很容易:
ls *[^s].txt
但是如何用一根绳子来做呢
塞巴斯蒂安和巴什
shopt -s extglob
ls !(*Thomas).txt
如果第一行表示“设置扩展全局”,请参阅以了解更多信息
其他一些方法可以是:
find . -type f \( -iname "*.txt" -a -not -iname "*thomas*" \)
ls *txt |grep -vi "thomas"
您可以使用“查找”来执行此操作:
$ find . -name '*.txt' -a ! -name '*Thomas.txt'
如果您正在循环一个通配符,只要跳过迭代的其余部分就可以了
for file in *.txt; do
case $file in *Thomas*) continue;; esac
: ... do stuff with "$file"
done
您可能需要添加
-maxdepth 1
以获得与简单通配符类似的行为
for file in *.txt; do
case $file in *Thomas*) continue;; esac
: ... do stuff with "$file"
done