Skip to main content
Solved

Delete all files & folders in directory except for one folder


Forum|alt.badge.img+3
  • New Contributor
  • 3 replies

!/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

Thank you

Best answer by ryan_ball

@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 original
Did this topic help you find an answer to your question?

9 replies

dan-snelson
Forum|alt.badge.img+28
  • Honored Contributor
  • 632 replies
  • January 14, 2019

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


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 3 replies
  • January 15, 2019

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.


Forum|alt.badge.img+12
  • Valued Contributor
  • 359 replies
  • January 15, 2019

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.


Forum|alt.badge.img+18
  • Contributor
  • 475 replies
  • Answer
  • January 15, 2019

@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.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 3 replies
  • January 15, 2019

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.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 3 replies
  • January 15, 2019

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


Forum|alt.badge.img+5
  • Contributor
  • 50 replies
  • August 7, 2021
ACM wrote:

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.


Forum|alt.badge.img+12
  • Valued Contributor
  • 156 replies
  • January 12, 2022
ryan_ball wrote:

@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 "+"


Forum|alt.badge.img+1
  • New Contributor
  • 3 replies
  • August 11, 2022
khurram wrote:

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 {} ;


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings