Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm sorry for asking such a basic question:

How do I delete everything in a directory, including hidden files and directories?

Right now, I use the following:

rm -rf *
rm -rf .*
share|improve this question
3  
You can combine them to rm -rf .* *. –  user unknown Mar 21 '11 at 3:38
add comment

8 Answers

The best answer is: Don't do that. Recursively remove the directory itself, then recreate it as an empty directory. It's more reliable and easier for other people to understand what you're trying to do. When you re-create the directory it may have a different owner, group and permissions. If those are important be careful. Also, if any running programs have open file handles (descriptors) on the directory then you won't want to remove it. If none of those special cases apply, then it's simpler to remove the entire directory.

share|improve this answer
4  
So how is that more easy. 'Be careful' isn't an answer. I wouldn't understand why somebody deletes a directory and rebuilds it again. –  user unknown Mar 21 '11 at 3:36
    
I added the phrase "as an empty directory", perhaps that's more clear. –  Chris Quenelle Jul 2 '13 at 22:49
    
No, it doesn't explain why you delete a directory and recreate it then. To the things to consider belongs, btw., date/time of creation too. –  user unknown Jul 2 '13 at 23:14
    
If that directory is the current working directory of some process, you may run into problems. Also, if you remove the directory, you remove information about its permissions and ownership. –  paraxor Jul 3 '13 at 0:03
add comment

Assuming bash 4+:

shopt -s dotglob
rm -rf -- *
##or:
rm -rf ./*

With dotglob enabled, * expands to all files and directories, even those that begin with . - but doesn't expand to . and .., so it is safe to use with rm.

share|improve this answer
add comment

Simple and straight forward:

find -delete 

Includes directories and hidden files. At least gnu-find knows -delete, your find may differ.

share|improve this answer
add comment

How about using find. I think this is generally a good choice, when you have to dig through sub-directories.

find . -type f -exec rm {} \;
share|improve this answer
    
there is a -delete switch, so you can delete directories. (at least in gnu-find). –  user unknown Mar 21 '11 at 3:40
add comment

Oh my Zsh

rm -rf (.|)*

Again, this is for Zsh only.

share|improve this answer
2  
Or *(D) (the D glob qualifier turns on the glob_dots option for this pattern). –  Gilles Jan 28 '11 at 20:14
add comment

if you are in the directory:

cd .. && rm -rf dir && mkdir dir && cd dir

otherwise:

rm -rf /path/to/dir && mkdir /path/to/dir

share|improve this answer
add comment
rm -rf -- * .[!.]* ..?*

Each of the three pattern expands to itself if it matches nothing, but that's not a problem here since we want to match everything and rm -f ignored nonexistent arguments.

Note that .* would match ...

share|improve this answer
add comment

Try rm -rf *?*. This will delete normal and hidden files.

share|improve this answer
4  
Not in any shell that I know of. –  Gilles Jan 27 '11 at 22:45
    
you are correct. it does not deletes sub-directories. –  gladimdim Jan 28 '11 at 18:03
3  
The problem is more likely to be that *?* will not match “dot” files/dirs (unless you have enabled the dotglob option in bash, the GLOB_DOTS option in zsh, or an equivalent for whatever shell you are using). –  Chris Johnsen Jan 29 '11 at 3:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.