112

I know this will delete everything in a subdirectory and below it:

rm -rf <subdir-name>

But how do you delete everything in the current directory as well as every subdirectory below it and the contents of all of those subdirectories?

Sam
  • 7,252
  • 16
  • 46
  • 65
Yen
  • 2,176
  • 3
  • 19
  • 27
  • 2
    This has to be a dupe: http://stackoverflow.com/questions/550922/how-to-delete-all-files-from-current-directory-including-current-directory – jmucchiello May 04 '09 at 19:16

10 Answers10

167

Practice safe computing. Simply go up one level in the hierarchy and don't use a wildcard expression:

cd ..; rm -rf -- <dir-to-remove>

The two dashes -- tell rm that <dir-to-remove> is not a command-line option, even when it begins with a dash.

Flimm
  • 136,138
  • 45
  • 251
  • 267
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 34
    Because you are specifically matching a named directory and are thus less likely to delete something that you don't intend to delete. – tvanfosson May 04 '09 at 16:27
  • 5
    True. I could see myself doing that pretty easily. – Yen May 04 '09 at 16:29
  • 28
    doesn't it delete the directory itself too? You have to do mkdir afterwards. But then any hardlink referring to that directory will be a distinct directory afterwards. – Johannes Schaub - litb May 04 '09 at 16:44
  • litb, I know I have a filesystem that allows hardlinks to directories, but I really doubt you have it too. – Joshua May 04 '09 at 17:26
  • Agree. Note: "rm -rf ." would leave you in an undertimate state with no current directory. Unix abhors an indeterminate state. – dkretz May 04 '09 at 18:27
  • @Joshua I have a filesystem that allows hardlinks to directories too; it's called HFS+ and anyone who is running Mac OS X 10.5 and higher has it too. – Brian Campbell May 04 '09 at 18:40
  • @Joshua, I wasn't aware that i can't do directory hard-links in linux with ext3. But i just tried and it didn't let me do it. Good point dude :) – Johannes Schaub - litb May 04 '09 at 19:01
  • To be extra safe, you need to make sure that `rm` doesn't interpret the directory name as an option: `cd ..; rm -rf -- ` – Flimm Dec 09 '11 at 15:23
  • finding this now after I just deleted EVERYTHING on my server. I'm new to unix, but have a background with DOS, and the syntax is a bit different so I made a mistake. I did "rm -rf /*" intending to delete everything in the current directory, but deleted everything in root. Rackspace is restoring us now. In the future I'll keep this advice and avoid wildcards! Thanks! – ChatGPT Aug 13 '12 at 09:45
  • 5
    @Yen, if you use `rm ./` you might accidentally type `rm . /` which could be disaster. – irfandar Oct 12 '12 at 17:38
  • @MaxHodges I did something similar today on my MacBook. I accidentally did "sudo rm -R /" when trying to delete the current directory. I almost had a heart attack! Luckily, by the time I hit + C, it had only deleted applications A through E in /Applications. I was able to get them back easily using Pacifist and the OS X Install disk. Whew... –  Mar 19 '13 at 16:37
  • 1
    hmm...i thought he wanted to delete everything in the current directory, but not the directory itself...how do we do that? – Tim Boland Mar 10 '17 at 01:15
47

Will delete all files/directories below the current one.

find -mindepth 1 -delete

If you want to do the same with another directory whose name you have, you can just name that

find <name-of-directory> -mindepth 1 -delete

If you want to remove not only the sub-directories and files of it, but also the directory itself, omit -mindepth 1. Do it without the -delete to get a list of the things that will be removed.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • I needed to delete all the files in sub-directories, but did not want to delete the sub-directories themselves. find -mindepth 2 -delete worked great! – Tim Dearborn May 14 '13 at 19:30
  • You need `-mindepth 1` if you are specifying a directory (`find -mindepth 1 -delete`). Otherwise Johannes is right it will not delete the current working directory (when using `find -delete`). – Weboide Jul 18 '13 at 23:51
  • IMO, this is the best answer, particularly in scripts. – wizonesolutions Jul 17 '15 at 14:00
  • 1
    I tried: `find -mindepth 1 -delete` but i got `illegal option -- m` but it worked great when i removed the mindepth option `find . -delete` – sharon May 06 '16 at 01:51
44

What I always do is type

rm -rf *

and then hit ESC-*, and bash will expand the * to an explicit list of files and directories in the current working directory.

The benefits are:

  • I can review the list of files to delete before hitting ENTER.
  • The command history will not contain "rm -rf *" with the wildcard intact, which might then be accidentally reused in the wrong place at the wrong time. Instead, the command history will have the actual file names in there.
  • It has also become handy once or twice to answer "wait a second... which files did I just delete?". The file names are visible in the terminal scrollback buffer or the command history.

In fact, I like this so much that I've made it the default behavior for TAB with this line in .bashrc:

bind TAB:insert-completions
Ville Laurikari
  • 28,380
  • 7
  • 60
  • 55
23

Use

rm -rf *

Update: The . stands for current directory, but we cannot use this. The command seems to have explicit checks for . and ... Use the wildcard globbing instead. But this can be risky.

A safer version IMO is to use:

rm -ri * 

(this prompts you for confirmation before deleting every file/directory.)

dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 14
    "Can be risky" is wondefully laconic. –  May 04 '09 at 16:19
  • 5
    When doing things like this, I've found a quick ls -r . first lets you see what you are going to delete. Useful to give a quick idea that you aren't going to delete the whole disk... – Rich Bradshaw May 04 '09 at 16:20
  • 2
    Why is it riskier than rm -rf ? – Yen May 04 '09 at 16:20
  • @liw.fi: My .bashrc typically has this: alias rm="rm -i". – dirkgently May 04 '09 at 16:22
  • 2
    @Yen -- because if you do it in the wrong place you can get disastrous results. Using a specific name in the wrong place can only go wrong if the same subdirectory happens to exist there. – tvanfosson May 04 '09 at 16:23
  • Is there any possibility that it follows ".." and tries to remove parent directories? Not that i would think it would, but i better don't try it out on my box. – Johannes Schaub - litb May 04 '09 at 16:31
  • 1
    You cannot possibly delete the parent while residing in the child. – dirkgently May 04 '09 at 16:32
  • When I tried to run this command it failed, saying: rm: cannot remove `.' or `..' – Yen May 04 '09 at 16:35
  • Then you are left with no other option but to use the * wildcard expression. – dirkgently May 04 '09 at 16:36
  • A long time ago, I read a wonderfully inspiring story of a "rm -rf *" executed in the root directory. The author caught it before it finished with /bin, and reconstructed the system using only commands late in the alphabet (the earlier ones having been deleted). – David Thornley May 04 '09 at 17:10
  • @David: You're probably thinking about this story: http://www.ee.ryerson.ca/~elf/hack/recovery.html – sth May 04 '09 at 18:26
  • Missing `--` in this answer? – Ben Voigt Mar 14 '14 at 15:50
7

It is correct that rm –rf . will remove everything in the current directly including any subdirectories and their content. The single dot (.) means the current directory. be carefull not to do rm -rf .. since the double dot (..) means the previous directory.

This being said, if you are like me and have multiple terminal windows open at the same time, you'd better be safe and use rm -ir . Lets look at the command arguments to understand why.

First, if you look at the rm command man page (man rm under most Unix) you notice that –r means "remove the contents of directories recursively". So, doing rm -r . alone would delete everything in the current directory and everything bellow it.

In rm –rf . the added -f means "ignore nonexistent files, never prompt". That command deletes all the files and directories in the current directory and never prompts you to confirm you really want to do that. -f is particularly dangerous if you run the command under a privilege user since you could delete the content of any directory without getting a chance to make sure that's really what you want.

On the otherhand, in rm -ri . the -i that replaces the -f means "prompt before any removal". This means you'll get a chance to say "oups! that's not what I want" before rm goes happily delete all your files.

In my early sysadmin days I did an rm -rf / on a system while logged with full privileges (root). The result was two days passed a restoring the system from backups. That's why I now employ rm -ri now.

Pierre-Luc Simard
  • 2,723
  • 19
  • 27
7

How about:

rm -rf "$(pwd -P)"/* 
5
rm  -rf * 

Don't do it! It's dangerous! MAKE SURE YOU'RE IN THE RIGHT DIRECTORY!

Ferdi
  • 540
  • 3
  • 12
  • 23
nkassis
  • 5,285
  • 2
  • 21
  • 22
4

make sure you are in the correct directory

rm -rf *
digitaljoel
  • 26,265
  • 15
  • 89
  • 115
  • 2
    This does not delete files or subdirectories whose name starts with a period. –  May 04 '09 at 16:18
  • 1
    true. in my testing, neither does rm -rf . tvanfosson has the best solution IMO with his "cd ..; rm -rf " – digitaljoel May 04 '09 at 16:28
2

This simplest safe & general solution is probably:

find -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf
devnev
  • 3,141
  • 1
  • 15
  • 10
1

I believe this answer is better:

https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory

If your top-level directory is called images, then run rm -r images/*. This uses the shell glob operator * to run rm -r on every file or directory within images.

basically you go up one level, and then say delete everything inside X directory. This way you are still specifying what folder should have its content deleted, which is safer than just saying 'delete everything here", while preserving the original folder, (which sometimes you want to because you aren't allowed or just don't want to modify the folder's existing permissions)

Community
  • 1
  • 1
Francisco Noriega
  • 13,725
  • 11
  • 47
  • 72