In an old version of Unix, the rm
command would delete directories if they were empty. From the Research Unix Eighth Edition man page for rm: "If an entry is a directory it is removed only if empty." I like this behaviour, so I've got this alias in my /etc/profile
: alias rm='rm -d'
I'm using the GNU coreutils version of rm
, in which -d
tells rm
to go ahead and remove directories if they're empty.
So far so good. This alias allows me to use rm
like the old days. However, I'd like to take it a step further. I'd like rm
to delete a directory even if the directory contains other directories, so long as directories are the only thing there. It doesn't matter how deep the directory structure goes, as long as there aren't any files in there, just empty (once you get to the bottom) directories, I'd like rm
to remove them all.
Could this be written as an alias which would still function as the regular rm
and delete any files passed to it?
rmdir -p
?mkdir -p a/b1/c2 && mkdir -p a/b2/c2; rmdir -p a/b1/c1;
, I'm still left with a, a/b2, and a/b2/c2. What I'd like is to pass just the top level (a in this case) torm
, and have everything below it removed.