Jump to content

Script to delete folder contents except two named subfolders


Recommended Posts

Posted

Hi all,

 

I need a script to look in to a folder and delete all it's contents apart from two named folders. This is running on Mac OS, so needs to be Unix/Linux type script, not Windows. I am using the following:

 

shopt -s extglob
cd /Users
rm -rf !(administrator)

 

...which removes everything apart from a folder called "administrator", however I want it to delete everything apart from "administrator" and "Shared" how do I structure the syntax to define a second exclusion?

 

I'm also open to any alternative methods...

 

Thanks

Posted
Hi all,

 

I need a script to look in to a folder and delete all it's contents apart from two named folders. This is running on Mac OS, so needs to be Unix/Linux type script, not Windows. I am using the following:

 

shopt -s extglob
cd /Users
rm -rf !(administrator)

 

...which removes everything apart from a folder called "administrator", however I want it to delete everything apart from "administrator" and "Shared" how do I structure the syntax to define a second exclusion?

 

I'm also open to any alternative methods...

 

Thanks

 

shopt -s extglob
cd /Users
rm -rf !(administrator[color=#008000]|shared[/color])

 

That should do the trick if it's using standard regex.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...