遍历文件并排除具有特定名称模式的文件 Shell
Iterate over files and exclude the ones with certain name pattern Shell
如何遍历当前目录下的文件并排除某些具有特定名称模式的文件?解决方案必须 POSIX 兼容。
假设要排除的文件遵循以下模式:test[0-9].txt 和 work-.*(使用正则表达式)。
我目前的代码:
for file in *
do
if test "$file" != "test[0-9].txt" -o "$file" != "work-.*"
then
echo "$file"
fi
done
目前输出的是工作目录中的所有文件。我很确定测试中的模式匹配不正确,但我该如何解决?
我想你想要:
if ! [[ "$file" =~ "test[0-9].txt" ]] -a ! [[ "$file" =~ "work-.*" ]]
[[
用于 bash,用于 POSIX shell,我想 case
可以为您进行 glob 样式匹配:
for file in *
do
case $file in
test[0-9].txt | work-*) ;;
*) echo "$file";;
esac
done
如何遍历当前目录下的文件并排除某些具有特定名称模式的文件?解决方案必须 POSIX 兼容。
假设要排除的文件遵循以下模式:test[0-9].txt 和 work-.*(使用正则表达式)。
我目前的代码:
for file in *
do
if test "$file" != "test[0-9].txt" -o "$file" != "work-.*"
then
echo "$file"
fi
done
目前输出的是工作目录中的所有文件。我很确定测试中的模式匹配不正确,但我该如何解决?
我想你想要:
if ! [[ "$file" =~ "test[0-9].txt" ]] -a ! [[ "$file" =~ "work-.*" ]]
[[
用于 bash,用于 POSIX shell,我想 case
可以为您进行 glob 样式匹配:
for file in *
do
case $file in
test[0-9].txt | work-*) ;;
*) echo "$file";;
esac
done