Delete all files & folders in directory except for one folder

ACM
New Contributor

!/bin/bash

rm -rf /Users/user/Documents/
rm -rf /Users/user/Downloads/

rm -rf /Users/user/Desktop/
rm -rf /Users/user/Pictures/

rm -rf /Users/user/Music/*

exit 0

*Hi all,

First time posting here, so apologies if this has already been posted before. We are currently using the script above to delete unwanted files created by students in our institution. However, we'd like to exclude a sub-directory called 'Apple Music Apps' within directory /Users/user/Music which has a file called 'LoopsDatabaseV09.db' which we don't want to delete.

Any help is greatly appreciated

d7a431267556427b8daa804260a01059

Thank you

1 ACCEPTED SOLUTION

ryan_ball
Valued Contributor

@sudhan Try this out.

First delete the files except the one you want to keep:

find /Users/user/Music ! -name "LoopsDatabaseV09.db" -type f -exec rm "{}" ;

Then delete the empty directories recursively:

find /Users/user/Music -type d -exec rmdir -p {} ;

This should leave LoopsDatabaseV09.db in the same directory it was originally located.

View solution in original post

9 REPLIES 9

dan-snelson
Valued Contributor II

@sudhan Completely untested, but something like rm -rf /Users/user/Music/* !('LoopsDatabaseV09.db') should work.

ACM
New Contributor

Hi @dan-snelson

Thank you for your instant reply. I tried your script and unfortunately, it's not working for us.

Please see the attached image.

Thank you once again.e6e474d668ee449ba8a71568aef6d68e

mschroder
Valued Contributor

I would set the system immutable flag for LoopsDatabaseV09.db with "sudo chflags simmutable $pathToFile". With that set even a "sudo rm -rf $pathToFile" will fail, and the file and its parent directories will survive. The downside is that the rm will create an error message.

A "sudo chflags nosimmutable $pathToFile" is needed to make the file 'removable' again.

ryan_ball
Valued Contributor

@sudhan Try this out.

First delete the files except the one you want to keep:

find /Users/user/Music ! -name "LoopsDatabaseV09.db" -type f -exec rm "{}" ;

Then delete the empty directories recursively:

find /Users/user/Music -type d -exec rmdir -p {} ;

This should leave LoopsDatabaseV09.db in the same directory it was originally located.

Giving error.

find /Users ! -name "administrator" -type d -exec rm "{}" ;

find: -exec: no terminating ";" or "+"

Mine is also giving error:

1:233: execution error: find: -exec: no terminating ";" or "+"
find: -exec: no terminating ";" or "+" (1)

Here is my script

#!/bin/bash

lastUser=`defaults read /Library/Preferences/com.apple.loginwindow lastUserName`

find /Users/$lastUser/Desktop ! -name "1.jpg" -type f -exec rm "{}" ;
find /Users/$lastUser/Desktop -type d -exec rmdir -p {} ;

ACM
New Contributor

Hi @mschroder ,

Thank you for getting back. I apologies, but what you are suggesting it bit beyond my basic knowledge of JAMF. But thank you anyway for your suggestion.

ACM
New Contributor

Hi @ryan.ball ,

IT WORKED!!!!!!!!! Thank you also for getting back. Really appreciate your help. Hope this is also helpful for anyone else who's having the same issue.

Best wishes,
Sudhan

Why are you trying to do this? The Loops should be a shared resource available to all users. It seems like you have multiple accounts that might be downloading multiple loops into personal accounts. The better move would be to move all loops into the shared loops directory.