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?

1

1 Answer 1

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.

1
  • I am using history expansion. How do I use the same command without conflicting that
    – RaceBase
    Oct 23, 2014 at 12:51

Not the answer you're looking for? Browse other questions tagged .