How do I remove all sub-directories from within a directory?

In BASH you can use the trailing slash (I think it should work in any POSIX shell):

rm -R -- */

Note the -- which separates options from arguments and allows one to remove entries starting with a hyphen - otherwise after expansion by the shell the entry name would be interpreted as an option by rm (the same holds for many other command line utilities).

Add the -f option if you don't want to be prompted for confirmation when deleting non-writeable files.

Note that by default, hidden directories (those whose name starts with .) will be left alone.

An important caveat: the expansion of */ will also include symlinks that eventually resolve to files of type directory. And depending on the rm implementation, rm -R -- thelink/ will either just delete the symlink, or (in most of them) delete the content of the linked directory recursively but not that directory itself nor the symlink.

If using zsh, a better approach would be to use a glob qualifier to select files of type directory only:

rm -R -- *(/) # or *(D/) to include hidden ones

or:

rm -R -- *(-/)

to include symlinks to directories (but because, this time, the expansion doesn't have trailing /s, it's the symlink only that is removed with all rm implementations).

With bash, AT&T ksh, yash or zsh you can do:

set -- */
rm -R -- "${@%/}"

to strip the trailing /.


In addition to the wildcard way, you can also use find (at least GNU find) to do this:

find -mindepth 1 -maxdepth 1 -type d -print0 | xargs -r0 rm -R

As with other find lines, you can run the first part (find -mindepth 1 -maxdepth 1 -type d) to see a list of directories that will be removed.

Portably (POSIXly), you'd use the -exec cmd {} + syntax instead of -print0 | xargs -r0, and -prune do limit the depth:

find . ! -name . -prune -type d -exec echo rm -R {} +

(and remove the echo to actually do it).

A safer option (here also assuming GNU mv) is to do something like this:

find -mindepth 1 -maxdepth 1 -type d -print0 | xargs -r0 mv -i -t ../to-rm
# or (but beware of symlinks!)
mv -i -t ../to-rm -- */
# or
mv -i -- */ ../to-rm

any of which will move all the stuff into ../to-rm instead of deleting it. You can verify it did what you wanted, them rm -Rf that directory at your leisure.


You might want to create a script for some of these suggestions, especially rm -R -- */, and keep it in your /usr/local/bin folder; or create an alias in your ~/.bashrc file. Since it's so easy to mistype a command and break your system -- even a single letter and/or the order of letters can result in catastrophic consequences -- this would serve as a somewhat more robust solution than having to type the different options and arguments each time you want to accomplish this task.

Also, you may want to include the -i or --interactive=once or -I or --interactive=always option to your script/command which will serve as another tool to avoid the unintended deletions.

Furthermore, something like derobert suggested would be best; just copy/paste the script into a file/terminal-editor and adjust it to your specific needs, and the files/directories will be moved into a single directory (the contents of which you can check/verify) that you can simply remove by issuing the rm -rf command.

Another option is to use a GUI-application, such as your file manager, and simply select all applicable files/folders you want to delete. Check your distribution's manual-pages if you don't have permissions.

Finally, if the folders are empty -- essentially simple filenames -- you can use the rmdir command to delete them. It doesn't work for everything you may want to delete, but it will come in handy at times when you want to do some "house-cleaning". **You may want try the -p --ignore-fail-on-non-empty option, which will allow you to delete certain sub-directories as well as their empty "parents"--the directories in which they reside.