0

I am trying to use rm -rf !(current) delete all folders in current path except current folder. But I am getting below error

bash: !: event not found

How do I get this functionality in bash?

Hennes
  • 65,804
  • 7
  • 115
  • 169
RaceBase
  • 693

1 Answers1

3

You need to enable extglob.

Suppose that we have three directories:

$ ls -d */
current/  future/  past/

Without extended glob, the following is not understood:

$ echo !(current)
bash: syntax error near unexpected token `('

If we enable extglob, then it is understood:

$ shopt -s extglob
$ echo !(current)
future past

This successfully matches all files except current.

Note that the exclamation point, !, is a bash-active character. It can invoke history expansion which, if it fails, results in the error that you observed. If you are not using history expansion, you may want to turn it off: set +H.

John1024
  • 17,343