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

macosterminal.app

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?

Best Answer

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
Related Question