In Unix, how do you remove everything in the current directory and below it?

210,209

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

How about:

rm -rf "$(pwd -P)"/* 
Share:
210,209

Related videos on Youtube

Yen
Author by

Yen

Updated on July 08, 2022

Comments

  • Yen
    Yen almost 2 years

    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?

  • Admin
    Admin almost 15 years
    This does not delete files or subdirectories whose name starts with a period.
  • Admin
    Admin almost 15 years
    "Can be risky" is wondefully laconic.
  • Rich Bradshaw
    Rich Bradshaw almost 15 years
    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...
  • Yen
    Yen almost 15 years
    Why is it riskier than rm -rf <subdir-name>?
  • dirkgently
    dirkgently almost 15 years
    @liw.fi: My .bashrc typically has this: alias rm="rm -i".
  • tvanfosson
    tvanfosson almost 15 years
    @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
    tvanfosson almost 15 years
    Because you are specifically matching a named directory and are thus less likely to delete something that you don't intend to delete.
  • digitaljoel
    digitaljoel almost 15 years
    true. in my testing, neither does rm -rf . tvanfosson has the best solution IMO with his "cd ..; rm -rf <dir-to-remove>"
  • Yen
    Yen almost 15 years
    True. I could see myself doing that pretty easily.
  • Johannes Schaub - litb
    Johannes Schaub - litb almost 15 years
    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.
  • dirkgently
    dirkgently almost 15 years
    You cannot possibly delete the parent while residing in the child.
  • Yen
    Yen almost 15 years
    When I tried to run this command it failed, saying: rm: cannot remove .' or ..'
  • dirkgently
    dirkgently almost 15 years
    Then you are left with no other option but to use the * wildcard expression.
  • Johannes Schaub - litb
    Johannes Schaub - litb almost 15 years
    doesn't it delete the directory itself too? You have to do mkdir <dir-to-remove> afterwards. But then any hardlink referring to that directory will be a distinct directory afterwards.
  • vpalmu
    vpalmu almost 15 years
    litb, I know I have a filesystem that allows hardlinks to directories, but I really doubt you have it too.
  • sth
    sth almost 15 years
    @David: You're probably thinking about this story: ee.ryerson.ca/~elf/hack/recovery.html
  • dkretz
    dkretz almost 15 years
    Agree. Note: "rm -rf ." would leave you in an undertimate state with no current directory. Unix abhors an indeterminate state.
  • Brian Campbell
    Brian Campbell almost 15 years
    @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.
  • Johannes Schaub - litb
    Johannes Schaub - litb almost 15 years
    @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 :)
  • Flimm
    Flimm over 12 years
    To be extra safe, you need to make sure that rm doesn't interpret the directory name as an option: cd ..; rm -rf -- <dir-to-remove>
  • Max Hodges
    Max Hodges over 11 years
    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!
  • irfandar
    irfandar over 11 years
    @Yen, if you use rm ./ you might accidentally type rm . / which could be disaster.
  • Admin
    Admin about 11 years
    @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 <CTRL> + 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...
  • Tim Dearborn
    Tim Dearborn almost 11 years
    I needed to delete all the files in sub-directories, but did not want to delete the sub-directories themselves. find <name-of-direcotry> -mindepth 2 -delete worked great!
  • Weboide
    Weboide almost 11 years
    You need -mindepth 1 if you are specifying a directory (find <name-of-directory> -mindepth 1 -delete). Otherwise Johannes is right it will not delete the current working directory (when using find -delete).
  • Pawel
    Pawel over 10 years
    jQuery is so powerful.
  • Ben Voigt
    Ben Voigt about 10 years
    Missing -- in this answer?
  • Ben Voigt
    Ben Voigt about 10 years
    Missing -- in this answer? This approach is awesome overall I must say.
  • wizonesolutions
    wizonesolutions almost 9 years
    IMO, this is the best answer, particularly in scripts.
  • sharon
    sharon almost 8 years
    I tried: find -mindepth 1 -delete but i got illegal option -- m but it worked great when i removed the mindepth option find . -delete
  • Tim Boland
    Tim Boland about 7 years
    hmm...i thought he wanted to delete everything in the current directory, but not the directory itself...how do we do that?
  • Abbas Torabi
    Abbas Torabi about 4 years
    better way is run this with sudo