Wednesday, August 30, 2006

Pruning Empty Directories

Just a quickie here. I had a fairly large directory structure that was pretty deep and had many empty directories, where an "empty" directory may still have subdirectories which are themselves empty (i.e. if I did mkdir -p foo/bar/baz, foo/ would be "empty"). The quickest way I found to clean up all the empty directories was to have find do a depth-first traversal (-d) of the directory structure, then use rmdir, which only deletes empty directories. Something like:

$ find -d . -print0 | xargs -0 rmdir 2> /dev/null

2 comments:

Langleys said...

Hello. ran across your blog looking for a solution to my undeletable files problem. They are directories that got into the trash; I can't move them out of the trash, because they just copy. I can't delete them because they are "locked." It came about as I was installing my new hard drive, deleting files off my external drive, and setting up new users. I'm not experienced with terminal..Any easy fixes for me?

Anonymous said...

you can always remove them by executing:

rm ~/.Trash/*

in the terminal.