I just deleted a ton of old Music files in OS X via the search interface, which proved to be nice and simple, but, I ran into a small problem; the directories containing those files were left on the drive. As they were empty, it was rather pointless to leave them there, cluttering up the view. So, I did some poking around on the Web and found some solutions that I tweaked to quickly eliminate the empty directories. The length of this post makes the process look more complicated than it really is.Important Note: These commands, while safe in their current form, could very easily cause havoc on your system should they be used in the wrong spot and/or mistyped, or modified. I take absolutely no responsibility if you use these on your system and something goes wrong. I strongly recommend you research each part of the command online or via the man pages before executing any of them.The first thing I did, was use the find command to locate all of the empty directories and subdirectories within my current directory, and print them on the screen:
find . -depth -type d -empty -exec echo {} ';'
I noticed that many directories that I thought were empty were not returned by this command. Some quick research via the command line showed that these supposedly empty directories had a hidden file in them. The file, .DS_Store is used by the finder to store the last view settings (view as columns/icons/list) so you see the same thing the next time you go to the folder. So, in order to ensure I could easily delete all of the "empty" directories, I had to eliminate these .DS_Store files. So, I ran the following command to get a list of every file that I was going to eliminate (I like to see them first):
find . -name .DS_Store -exec echo {} ;
Upon seeing the returned items, I noticed that this will delete every single .DS_Store within the current directory and its subdirectories, even in the ones that are not empty. But, that isn't a concern, as the finder will create a new .DS_Storefile the next time a folder is viewed. So, satisfied with the list, I used this command to eliminate all of them:
find . -name .DS_Store -exec rm {} ;
Once all of the .DS_Store files were eliminated, I re-ran the empty directory search:
find . -depth -type d -empty -exec echo {} ';'
It returned all of the directories that I expected, so I ran this final bit of code to get rid of all of the now, truly-empty directories (be very careful that you type this properly):
find . -depth -type d -empty -exec rmdir {} ';'
I now have a clean Music directory. It's the little things in life that often bring happiness.