linux rm command to exclude files

#排除a.txt 和b.txt文件
​​​​​​​rm -rf !(a.txt|b.txt)

will appear after execution

-bash: !: event not found

Reason:
#shopt extglob is not enabled

shopt -s extglob  #-s开启 -u关闭
#shopt命令和extglob是什么东东

shopt command

shopt(选项)(参数)

#选项
#-s:激活指定的shell行为选项; 
#-u:关闭指定的shell行为选项。
#参数
cdable_vars|cdspell|checkhash|checkwinsize|cmdhist|dotglob|......
等等用时自行查找

The shopt command is used to display and set behavioral options in the shell that enhance shell usability. If the shopt command does not take any parameter options, it can display all the shell operation options that can be set.
From:  http://man.linuxde.net/shopt

 

The Bash Shell has an extglob option. After it is turned on, the Shell can additionally recognize 5 pattern matching operators, which can make file matching more convenient.
The opening method is very simple, use the shopt command: shopt -s extglob to 
turn off, use the shopt command: shopt -u

After extglob is turned on, the following 5 pattern matching operators will be recognized:

  1. ?(pattern-list) - match the given pattern 0 or 1 times;
  2. *(pattern-list) - The given pattern is matched more than 0 times, including 0 times;
  3. +(pattern-list) - The given pattern is matched more than 1 time, including 1 time;
  4. @(pattern-list) - the given pattern is matched only once;
  5. !(pattern-list) - does not match the given pattern within parentheses.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325566708&siteId=291194637
Recommended