遍历文件并排除具有特定名称模式Shell的文件



如何迭代当前目录中的文件并排除一些具有特定名称模式的文件?该解决方案必须与POSIX兼容。

假设要排除的文件遵循以下模式:test[0-9].txt和work-.*(使用regex(。

到目前为止我拥有的代码:

for file in * 
do 
if test "$file" != "test[0-9].txt" -o "$file" != "work-.*"
then 
echo "$file"
fi 
done 

目前的输出是工作目录中的所有文件。我确信测试中的模式匹配是不正确的,但我该如何修复它呢?

[[用于bash,对于POSIX shell,我想case可以为您进行glob样式的匹配:

for file in *
do
case $file in
test[0-9].txt | work-*) ;;
*) echo "$file";;
esac
done

我想你想要:

if ! [[ "$file" =~ "test[0-9].txt" ]] -a ! [[ "$file" =~ "work-.*" ]]

相关内容

最新更新