1

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?

0

2 Answers 2

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 .