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

165,508

Solution 1

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

Solution 2

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.

Solution 3

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.

Share:
165,508
Evster
Author by

Evster

Updated on September 18, 2022

Comments

  • Evster
    Evster almost 2 years

    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.

  • Evster
    Evster over 11 years
    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.
  • peterph
    peterph over 11 years
    -r, -R, --recursive are synonyms meaning "remove directories and their contents recursively".
  • amphibient
    amphibient over 11 years
    can you explain what -- and */ do?
  • Evster
    Evster over 11 years
    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
    Evster over 11 years
    So to confirm my question above... -r, -R and --recursive all mean the same thing? There is no difference between typing any of these commands?
  • peterph
    peterph over 11 years
    @Evster yes, at least for rm from coreutils. It might vary for other implementations though (always see man/info pages).
  • Evster
    Evster over 11 years
    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
    Evster over 11 years
    Just caught your edit with the addition of the --. I follow what you're saying, but am I correct in stating that the command can be run successfully even without the -- so long as there are NO directories beginning with a hyphen (-)? Your original command worked fine for me, but I never begin my directory names with hyphens.
  • frostschutz
    frostschutz over 11 years
    Yes, -- is just to stop hyphen-names to be interpreted as command line options.
  • Evster
    Evster over 11 years
    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!
  • ILMostro_7
    ILMostro_7 over 11 years
    -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.
  • l0b0
    l0b0 over 11 years
    You can also say rm -R ./*/ - It also avoids problems with hyphen names.
  • Paulo Tomé
    Paulo Tomé over 8 years
    Simple, therefore beautiful.
  • Stephen Rauch
    Stephen Rauch over 7 years
    The OP asked to keep the contents of the current directory.
  • ChrisOdney
    ChrisOdney almost 7 years
    Just be careful not to change the order of the * and the /. rm -R -- /* does something totally different that you may not want to happen :|
  • Stéphane Chazelas
    Stéphane Chazelas almost 7 years
    What would be the benefit over rm -rf -- */? printf '%s\0' */ | xargs -r0 rm -rf -- would help (in shells where printf is builtin and not ls) to work around arg list too large errors, but ls -d */ | xargs rm -rf doesn't add anything AFAICT except additional problems.
  • Anthony Geoghegan
    Anthony Geoghegan almost 7 years
    @StéphaneChazelas is correct. The first thing I noticed with this answer is that it would break if there are directories with spaces in their names. This command won't even report the problem because the -f option instructs rm to ignore nonexistent files and arguments.
  • Ankit Marothi
    Ankit Marothi almost 7 years
    @StéphaneChazelas: Thank you! Not aware, of how rm -rf -- */? and printf '%s\0' */ | xargs -r0 rm -rf -- are better. Any explanation on that would be helpful. @AnthonyGeoghegan: Thank you! I didn't know about it.
  • Stéphane Chazelas
    Stéphane Chazelas almost 7 years
    @AnthonyGeoghegan, yes for instance if there was a directory called " .." (space ..), it would remove the content of the parent directory with some ls implementations. And it's not only spaces, it's also other blank characters, newline, single quote, double quote, backslash and file names starting with -.
  • Ankit Marothi
    Ankit Marothi almost 7 years
    @StéphaneChazelas: Thank you! I will go through it!
  • Anthony Geoghegan
    Anthony Geoghegan almost 7 years
    When you fully understand @StéphaneChazelas' suggestion of using printf to generate a list where each name is terminated by the null character so it can be processed by xargs with the -0 option, I’d advise that you edit your answer to update it with his variation. I’d be fairly confident that he’d be happy for you to use his code snippet if you understand it and have learned how it guards against undesirable behaviour caused by certain file names that could potentially exist.
  • Anthony Geoghegan
    Anthony Geoghegan almost 7 years
    Using xargs does have one advantage over the accepted answer (it works when there's a very large number of files/directories in the current directory) so there’s scope for editing this into a useful answer. Personally, I’ve found answering Stack Exchange questions to be a good learning experience – particularly when I get useful feed-back from other users.
  • Арсений Черенков
    Арсений Черенков almost 4 years
    OP want to keep top level directory.