1

This question already has an answer here:

I want to delete all files and directories from a specific directory except for one directory. For the same operation I had run this command

# rm -R -df !(dir_name_not_to_delete)

It works in Ubuntu but it gives an error in CentOS; i.e.,

-bash: !: event not found  " what is the reason for this error"

What do I have to use in CentOS to get this to work? Is there any alternative of '!' this sign in Centos?

marked as duplicate by DavidPostill, fixer1234, MariusMatutiae, Scott, Dave Jun 30 '15 at 11:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

First, !(dir_name_not_to_delete) is an extended glob. For it to work, you need to have the extended globs option turned on. Run:

shopt -s extglob

Secondly, the specific error message that you see, -bash: !: event not found, means that you also have history expansion turned on. If you don't want history expansion, it it safest to turn it off:

set +H
0

Ok how about list everything in that folder then exclude that folder and delete everything else

find MYDIR -not -name EXCEPT_DIR -exec rm -rf {}\;

But before doing that, just test what the find command will find, ex:

find MYDIR -not -name EXCEPT_DIR

Not the answer you're looking for? Browse other questions tagged or ask your own question.