Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question is kind of a phase II to the first question I posted at here

I have a directory that contains a bunch of sub-directories, .zip files, and other random files not contained within a sub-directory.

I'd like a command line script to remove all sub-directories from within the parent directory, but keep all zip files and loose files that don't belong to any sub-directories. All of the sub-directories have content, so I believe I'd need to force their deletion with the -f command.

So basically, a command that looks inside the parent directory (or the current directory), deletes all folders from within it, but keeps all other content and files that are not a folder or contained within a folder.

I understand that deleting items from the command line requires special care, but I have already taken all necessary precautions to back up remotely.

share|improve this question
up vote 25 down vote accepted

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

rm -R -- */

Note the -- (thanks for adding that, Stephane) 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).

share|improve this answer
Beautiful! This works perfectly. I didn't realize it was so easy to do. Would you mind elaborating on the -r vs. -R and what those commands do? Many thanks. – Evster Mar 22 '13 at 21:28
-r, -R, --recursive are synonyms meaning "remove directories and their contents recursively". – peterph Mar 22 '13 at 21:39
can you explain what -- and */ do? – amphibient Mar 22 '13 at 21:42
I'll take a stab at amphibient's question: the * character is a wildcard character, meaning that it will match any set of characters the comprises the directory name(s). The / signifies that the files to look for are directories (as opposed to .jpg, .txt, .sql, etc...) – Evster Mar 22 '13 at 21:47
You can also say rm -R ./*/ - It also avoids problems with hyphen names. – l0b0 Mar 23 '13 at 9:08

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 -0 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.

A safer option is to do something like this:

find -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 mv -i -t ../to-rm
# or
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.

share|improve this answer
+1 for the "CLI-Trash bin" to-rm and find which nicely handles directories beginning with a dot (in addition to any other cumbersome escaping). :) – peterph Mar 22 '13 at 22:24
Thanks for the tip. I like the idea of moving to a separate directory before deleting. I'm working via Terminal on Mac OS X, and I believe I'd need to install some kind of GNU package to use the -mindepth and -maxdepth commands, correct? – Evster Mar 22 '13 at 22:25
@Evster not sure if Mac OS X find supports those. I'd guess fink (is that still around?) could install a find that does. Or you could omit them, and then filter the produced file list using grep. Definitely use the mv option on that, istead of rm. – derobert Mar 22 '13 at 22:52

you might wanna 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 sooo 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 distro'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 wanna 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.

share|improve this answer
That's a really good idea on creating a separate script I can call for this command. Can you explain what exactly the -i and -I options do? Thanks! – Evster Mar 23 '13 at 0:33
-I option will prompt you once after you hit "Enter" to confirm that you want to remove this(when there are more than 3 files/folders "selected"); whereas the -i option will issue a prompt for each file, upon which you can simply confirm by tapping "y" and "Enter"--anything besides an affirmative response will result in that specific file being skipped during the removal process. – ILMostro_7 Mar 23 '13 at 0:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.