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

Right now, I use the following:

rm -rf *
rm -rf .*
  • 5
    You can combine them to rm -rf .* *. – user unknown Mar 21 '11 at 3:38
  • 2
    You can go one directory up and then run rm -rf yourdirectory/* – shreyansp Apr 5 '16 at 9:08
  • 1
    sorry missed the hidden directory part. The previous users solution covers that – shreyansp Apr 5 '16 at 9:15
  • That second option you posted is very dangerous. It will match the .. directory, which will delete whatever is in the directory above. – Richard Peterson Dec 28 '16 at 18:56

Simple and straight forward:

find -delete 

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

| improve this answer | |
  • 1
    To avoid deleting the directory, use find /path -delete -mindepth 1. – orodbhen Jun 9 '17 at 11:29
  • @orodbhen: find -delete won't delete the directory, where you launch the command (as in the question with rm). – user unknown Jun 10 '17 at 9:07

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.

| improve this answer | |
  • 7
    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
  • 2
    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
  • 2
    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. – user26112 Jul 3 '13 at 0:03
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 ...

| improve this answer | |

Assuming bash 4+:

shopt -s dotglob
rm -rf -- *
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.

| improve this answer | |

if you are in the directory:

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


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

| improve this answer | |

Oh my Zsh

rm -rf (.|)*

Again, this is for Zsh only.

| improve this answer | |
  • 2
    Or *(D) (the D glob qualifier turns on the glob_dots option for this pattern). – Gilles 'SO- stop being evil' Jan 28 '11 at 20:14
  • Or rm -rf {.,}* (unlike bash, zsh doesn't include . and .. into {.,}*, at least on my machine). – sasha Jan 11 '17 at 11:50

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 {} \;
| improve this answer | |
  • 1
    there is a -delete switch, so you can delete directories. (at least in gnu-find). – user unknown Mar 21 '11 at 3:40
  • this won't delete subdirectories – sasha Jan 11 '17 at 11:43

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

| improve this answer | |
  • 4
    Not in any shell that I know of. – Gilles 'SO- stop being evil' Jan 27 '11 at 22:45
  • you are correct. it does not deletes sub-directories. – gladimdim Jan 28 '11 at 18:03
  • 4
    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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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