OS X Find and RM recursively through folders with spaces in the names?

5

2

WD My Cloud NAS added hidden .wdmc folders to every folder with a jpg without notifying or asking me first. There are many hundreds on my NAS drive. It has something to do with their media server but I never turned that feature on. These folders are packed with data that bloats my backups and disk space usage. They have to be deleted but they are all over the disk and sometimes deep inside directory structures.

On Superuser.com I found this advice, ran it in Terminal, and it appeared to spend all night deleting these folders. However, it didn't delete the target folders or the data in them.

$ find . -type d -name '.wdmc' -print -exec echo rm -rf {} \; 

I also found this but it only worked for folders with no spaces in the name:

rm -rf `find . -type d -name .wdmc`

Somehow in the path there must be a way to escape the spaces in folder names?

Preston

Posted 2014-08-04T22:01:17.657

Reputation: 153

I'm going to post the solution, if any, in my Amazon review for WD My Cloud NAS disks. My guess is that a lot of the product's 5 star reviews would be much less if users saw the mess that it causes. – Preston – 2014-08-05T22:31:47.670

I finally gave up and used the second statement above to remove directories within folder trees when all folders had no spaces in the names. Then I used find . -type d -name .wdmc to find the remaining files, which was most of them, copied the results from Terminal to a text editor so I could narrow my search then used Finder with hidden files exposed to delete them. This ate piles of time but saved over 200 GB of space, made moving and deleting folders faster, and made cloud backup vastly faster. I detest Western Digital. – Preston – 2014-08-06T23:40:11.127

Answers

6

Version 2 (piping via xargs)

After digging around in the manpage for find (see for example here) I found a solution that uses the print0 option and xargs to pipe the directory names to rm:

$ find . -type d -name '.wdmc' -print0 | xargs -0 rm -rf

This should also work for directory names containing spaces or other, non-ASCII characters.

Version 1 (only works for empty .wdmc directories)

Alternatively you could use the -delete option of find:

$ find . -type d -name '.wdmc' -delete

hakaze

Posted 2014-08-04T22:01:17.657

Reputation: 196

This doesn't work, and I tried again, because -delete will not remove non-empty folders. rm -r travels through those folders and deletes the contents before deleting the folder. rm -r works fine except it hates spaces in folders. Somehow I have to escape those spaces but I can't figure that out. I've done a lot of searching but all the suggestions I've found are for simple directory trees with names without spaces. – Preston – 2014-08-05T22:29:36.130

YES! Version 2 above reaches deep into the directory trees and snuffs these folders and the files in them. Thank you! As word gets out that Western Digital carpet bombs storage with unwanted files without user permission or knowledge I'm sure this code will be appreciated by many. By the way, it took almost all day for me to test your script on just one directory. The My Cloud drive is a slug. I get better performance with a USB 2 HD attached to my Time Capsule. – Preston – 2014-08-07T23:19:48.737

Remove -print0 and it prints activity :-) – Preston – 2014-08-08T00:27:49.280

Or use xargs -0t ;-) – hakaze – 2014-08-08T05:14:06.673

Yes, -0t works better. It wasn't always printing to the screen before. – Preston – 2014-08-08T16:25:11.787

Strange, version 2 works but this doesn't: find . -type d -name '.wdmc’ | xargs -0 rm -rf [return]

Notice this prompt >. Nothing happens. I also added the -0t but no change. For some reason the -print needs to be there but it doesn't print to the screen any activity. I have to use this to see if it worked: find . -type d -name .wdmc

– Preston – 2014-08-10T00:31:11.727

@jmpreston you need -print0 to properly format the results for the subsequent handling by xargs! See the find man-page!

– hakaze – 2014-08-12T05:05:09.860

I read the manual page for find several times but never noticed that. Or at least didn't understand that. Thanks for pointing it out! – Preston – 2014-08-12T16:07:12.687

0

Have you tried:

$ find . -type d -name '*.wdmc' -print -exec echo rm -rf "{}" \; 

It works for me on my mac testing it with ls instead of rm.

Marcelo

Posted 2014-08-04T22:01:17.657

Reputation: 808

Both of the methods I tried will find the .wdmc folders which are just like system folders. The problem is the path right after find. I need to search and delete inside a lot of folders and many are several deep in the directory structure. Find . won't search and remove in /foo bar/ but it will find the folders I want to remove.

I tried your suggestion and the folders to be deleted are printed with path names but not removed. That is what happened with the first command I tried. I'm no command line wiz by the way :-) Complicated. – Preston – 2014-08-04T22:28:14.357

0

You can use your finder-app too.

there is a search field in the upper right corner just type *.wdmc.

by default the search scope is your whole mac, but you can change it when you start the search. switch to your curent folder if you was on the top most directory the finder will recursively search for all files in this folder

than you can mark them all witch command+a and delete them

chris

Posted 2014-08-04T22:01:17.657

Reputation: 71

Thanks Chris but Finder doesn't work. No results even when in the folder with a .wdmc file. I can use Finder for searching through the folders, discovering, and deleting these folders but that is a huge time sink. Of course I have to use Terminal to fix Finder so it shows hidden files. – Preston – 2014-08-05T14:12:26.760

you could try this:Type the following into a terminal window:

 defaults write com.apple.finder AppleShowAllFiles 1 from https://discussions.apple.com/thread/5483892 sory i have no OSX-Machine here to test this too :(
 – chris  – 2014-08-05T14:41:34.677

Yeah, as I mentioned above I did that. I can see these hidden folders in Finder and can delete them but Western Digital puts many hundreds of them in folders everywhere there is a jpg. I would spend many days finding and deleting them with Finder. Professional photographers complain about finding thousands of these folders! I'm about ready to toss the My Cloud NAS into the garbage and buy another brand. – Preston – 2014-08-05T15:32:18.813

i dont know you folder struckture, but maybe i could be a good idea to delete the content in the folders and after that you could remove the write permission with chmod. – chris – 2014-08-05T20:48:18.873

That would be a huge time sink also. Better to buy another drive and restore to that from my cloud backup. Probably not Western Digital again. – Preston – 2014-08-05T22:07:00.687

what are your requirements for an storage? Personal i realy like the WD-Hardware but your problem show me, that i could be better not to have any WD-Software ^^' – chris – 2014-08-06T04:52:10.360

I no longer trust WD devices. For instance, I have one folder with 18 jpgs but their hidden folder in that folder has 58 jogs!! My storage setup is an Apple Time Capsule and CrashPlan for backups and a hard drive attached to the Time Capsule for stuff I don't need on my MacBook Pro but would like to have easily available. I'll probably use the My Cloud in off-site storage and replace it with a USB drive. Most NAS drives are a big security risk. – Preston – 2014-08-08T19:40:55.653