1.确认extglob 已经开启
shopt extglob
如果是off,开启extglob shopt -s extglob
shopt, -s 开启, -u 关闭
2.新建几个文件和文件夹进行测试
#touch main.c main.h hello.c hello.h
#mkdir dir1 dir2
保留hello.c hello.h dir1
#rm -rf !(hello.c|hello.h|dir1)
可以用*匹配,如:
rm -rf !(kx*) 保留所有kx开头的文件和文件夹
感叹号后不能有空格。