Tell me more ×
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
You can combine them to rm -rf .* *. – user unknown Mar 21 '11 at 3:38

7 Answers

The best answer is: Don't do that. Recursively remove the directory itself, then recreate it. 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.

share|improve this answer
3  
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
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

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

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

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

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

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
1  
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

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.