Use an 'rmdir' function that's smart about .DS_Store files

Dec 22, '09 07:30:01AM

Contributed by: luomat

Ever wish that 'rmdir' was smart enough that it would delete a directory if it was empty except for a .DS_Store file?

I wrote a zsh function which will replace rmdir and do exactly that. It is also available as a bash script, which you can rename to rmdir and drop in your $PATH.

The code is thoroughly documented for anyone who wants to see how it works, but the executive summary is this:

  1. Take a bunch of arguments
  2. Check to see if those arguments translate to directories
  3. If they do, check to see if the directory is emptied by /bin/rmdir
  4. If /bin/rmdir fails, check to see if the only file in the folder is .DS_Store
  5. If yes, delete the file and re-run /bin/rmdir on the folder
  6. If no, just report the directory is not empty
[robg adds: I've duplicated both files here on Mac OS X Hints, in case the source ever goes away: zsh function | bash script. I have not tested these myself.]

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20091215071544343