224

In bash all I know is that

rmdir directoryname

will remove the directory but only if it's empty. Is there a way to force remove subdirectories?

Piper
  • 2,535
  • 2
  • 19
  • 15

3 Answers3

326

The following command will do it for you. Use caution though if this isn't your intention as this also removes files in the directory and subdirectories.

rm -rf directoryname
Phillip Nordwall
  • 3,491
  • 1
  • 15
  • 6
  • 71
    "-f" is "--force" which overrides some sanity checks and prompting. A safer command to start with would be `rm -r directoryname`. – Jim Paris Aug 17 '12 at 03:35
  • 1
    For some reason I get a `rm: invalid option -- r` error when trying to delete a directory with rm -r . – Sunspawn May 26 '15 at 15:58
  • @Sunspawn [Check this related question](https://serverfault.com/questions/37823/how-to-delete-file-with-option-character-in-name-in-linux) to see if that's the case. – Marc.2377 Jul 03 '16 at 09:50
  • 1
    @JimParis I think the word "safer" is relative. Suppose you are writing a script to run on a remote computer. That script has a command which is supposed to remove a directory. Here, it would be "safer" to use `rm -rf directoryname` coz you wouldn't want your script to pause execution, because it's waiting for user input. Of course, you have to be sure that deleting the directory would do no harm. – John Red Sep 28 '16 at 10:08
  • Note that this will also delete files (as opposed to subdirectories). – Skippy le Grand Gourou Apr 21 '22 at 15:32
8

if rm -rf directoryname fails you, try using rm -R -f directoryname, or rm --recursive -f directoryname.

If you are not having any luck with these, you should consider reinstalling rm or switching shells.

saterHater
  • 372
  • 4
  • 10
  • These were the options available on my rm man page, I looked it up by typing `man rm` to view my options on recursive deletion and the _force_ options. – saterHater Nov 11 '15 at 18:38
  • Does your `rm` man page list `-r`?  What does it do?  (Try it in a directory that you create just for testing purposes, with only dummy files (and maybe subdirectories) in it.)  What operating system are you using? – G-Man Says 'Reinstate Monica' Nov 11 '15 at 19:08
  • 1
    P.S. If `rm -r` doesn't work, that would be an OS issue, not a shell issue. (Strictly speaking, it would be an issue with the version of `rm` that you're using, so you could address it by installing a different version of `rm`, or searching your system to see whether you already have a different version of `rm` in some directory other than `/bin`.) – G-Man Says 'Reinstate Monica' Nov 11 '15 at 21:34
  • Ah, right. I forgot to mention I'm on Ubuntu 14.04 When I ran `man rm` in my terminal, it gave me a text file with the `less` text viewer. I scrolled found an indented entry with a whole that had the _-R_ and _--recursive_ options cozied up with the _-r_ option, signifying that all of those arguments are identical. – saterHater Nov 11 '15 at 22:38
  • 1
    *edit: have you tried `sudo rm -r directoryName`*? The unwritten rules of the basic commands is that `-r` will allow a program to run recursively on every file your filesystem (starting where ever you choose!) and that -f will forcefully do things, even if it's _dangerous_. 'cd', 'mv', 'ls' _mostly_ holds this principle true. `ls -r /` is gonna be a duzie, and `cp -rf / /dev/null` will destroy everything on your filesystem. <--Never run that command! – saterHater Nov 11 '15 at 22:49
  • What would make `rm -Rf dir` succeed when `rm -rf dir` fails? On GNU systems, and other systems where the non-standard `-R` option is implemented, `-r` and `-R` (and `--recursive`) are the one and same option. – Kusalananda May 05 '22 at 16:58
3

Other answers show how to completely remove a directory’s content, but IMO they don’t address the literal question of the original post — that is, how can one delete subdirectories (as opposed to usual files). In other words, how can one delete empty directory structures while keeping subdirectories containing files ?

This can be achieved with find :

find directoryname -type d -delete

This command will recursively search for directories (-type d) through directoryname and -delete them only if their subdirectories or themselves don’t contain any files.