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
  • 64,768
  • 7
  • 111
  • 168
RaceBase
  • 633
  • 4
  • 11
  • 24
  • http://stackoverflow.com/questions/11081379/using-find-to-delete-all-files-with-a-given-name-except-the-one-with-a-given-ext – akira Oct 22 '14 at 06:34

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
  • 16,593
  • 5
  • 50
  • 45