有没有办法处理invers bash v4 shell扩展,即。处理所有文件不像通配符?在这种情况下,我需要rm
所有不具有'Folder-???'
格式的文件,并且想知道是否有更短的(即内置)方式然后执行
for file in *
do
[[ $i =~ \Folder-...\ ]] && rm '$i'
done
环。 (这个例子不起作用,顺便说一句......)
刚从bash学习好奇心......
答案 0 :(得分:14)
shopt -s extglob
rm -- !(Folder-???)
答案 1 :(得分:3)
@Dimitre对bash有right answer。便携式解决方案是使用case
:
for file in *; do
case "$file" in
Folder-???) : ;;
*) do stuff here ;;
esac
done
另一种bash解决方案是使用[[ ... ]]
(documentation)内的模式匹配运算符
for file in *; do
if [[ "$file" != Folder-??? ]]; then
do stuff here
fi
done
答案 2 :(得分:3)
如果您对find
感到满意,可以使用find
反转-not
的谓词。
在
中find -not -name 'Folder-???' -delete
(注意这与shell glob匹配的其他差异之间递归匹配,但我认为你已经足够了解find
)