427

From a terminal window:

When I use the rm command it can only remove files.
When I use the rmdir command it only removes empty folders.

If I have a directory nested with files and folders within folders with files and so on, is there a way to delete all the files and folders without all the strenuous command typing?

If it makes a difference, I am using the Mac Bash shell from a terminal, not Microsoft DOS or Linux.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Just in case you wish to restore the files in future , don't use "rm" for such cases . Use "rm-trash" : https://github.com/nateshmbhat/rm-trash – Natesh bhat Nov 20 '18 at 14:27

4 Answers4

959
rm -rf some_dir

-r "recursive" -f "force" (suppress confirmation messages)

Be careful!

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • 39
    +1 and glad you added the "Be careful!" part... definitely a "Sawzall" command that can quickly turn a good day into a bad one.. if wielded carelessly. – itsmatt Apr 15 '10 at 01:30
  • 7
    like dont do "# rm -rf /" you ll regret that: – DarthVader Apr 15 '10 at 01:35
  • 1
    @itsmatt: You know what they say...give someone a Sawzall, and suddenly every problem looks like hours of fun! – Jim Lewis Apr 15 '10 at 02:23
  • 199
    On a Mac? Do this instead: `brew install trash` then `trash -rf some_dir` This will move the unwanted directory into your trashbin instead of just vanishing _Prestige_-style into the ether. ([source](http://hasseg.org/trash/)) – zakdances Sep 02 '13 at 19:07
  • 1
    I get "Permission denied" – dev4life Aug 19 '15 at 21:10
  • 1
    If you get "Permission denied" use `sudo rm -rf some_dir` – Whitecat Jan 01 '16 at 20:43
  • Error: Cowardly refusing to 'sudo brew install' You can use brew with sudo, but only if the brew executable is owned by root. However, this is both not recommended and completely unsupported so do so at your own risk. – Chen Li Yong Sep 15 '16 at 09:58
  • To remove many directory with the same name (in this case .wdmc) recursively, go find . -iname ".wdmc" -exec rm -rfv {} + – Nico Jan 19 '19 at 16:10
  • make sure you are not in a modified interactive bash session as it might lead to a different outcome – Wojtek Dmyszewicz Nov 23 '20 at 23:23
86
rm -rf *

Would remove everything (folders & files) in the current directory.

But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.

Prine
  • 12,192
  • 8
  • 40
  • 59
19

Yes, there is. The -r option tells rm to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively an rmdir.

The other two options you should know are -i and -f. -i stands for interactive; it makes rm prompt you before deleting each and every file. -f stands for force; it goes ahead and deletes everything without asking. -i is safer, but -f is faster; only use it if you're absolutely sure you're deleting the right thing. You can specify these with -r or not; it's an independent setting.

And as usual, you can combine switches: rm -r -i is just rm -ri, and rm -r -f is rm -rf.

Also note that what you're learning applies to bash on every Unix OS: OS X, Linux, FreeBSD, etc. In fact, rm's syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.

Antal Spector-Zabusky
  • 36,191
  • 7
  • 77
  • 140
  • 1
    Although all the options mentioned above are standard among all the Unix flavors listed, there are some differences too. For example, OS X (but not Linux) has `"rm -d"`, which removes either files or empty directories. Still, +1 for the point that OS X is BSD internally. – David Gelhar Apr 15 '10 at 01:44
  • True---the *POSIX subset* is (pretty much) guaranteed to work consistently across Unixen, but anything outside that may or may not. – Antal Spector-Zabusky Apr 15 '10 at 02:11
4

I was looking for a way to remove all files in a directory except for some directories, and files, I wanted to keep around. I devised a way to do it using find:

find -E . -regex './(dir1|dir2|dir3)' -and -type d -prune -o -print -exec rm -rf {} \;

Essentially it uses regex to select the directories to exclude from the results then removes the remaining files.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
msantoro12
  • 126
  • 4