29

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

Right now, I use the following:

rm -rf *
rm -rf .*
4
  • 5
    You can combine them to rm -rf .* *. Mar 21, 2011 at 3:38
  • 2
    You can go one directory up and then run rm -rf yourdirectory/*
    – shreyansp
    Apr 5, 2016 at 9:08
  • 1
    sorry missed the hidden directory part. The previous users solution covers that
    – shreyansp
    Apr 5, 2016 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. Dec 28, 2016 at 18:56

8 Answers 8

19

Simple and straight forward:

find -delete 

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

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

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.

6
  • 8
    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. Mar 21, 2011 at 3:36
  • I added the phrase "as an empty directory", perhaps that's more clear. Jul 2, 2013 at 22:49
  • 3
    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. Jul 2, 2013 at 23:14
  • 3
    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, 2013 at 0:03
  • What if the directory is a mount point? What if you don't have permission to remove the directory itself? Jan 20, 2022 at 19:08
9
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 ...

0
5

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.

2

if you are in the directory:

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

otherwise:

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

2

Oh my Zsh

rm -rf (.|)*

Again, this is for Zsh only.

2
  • 2
    Or *(D) (the D glob qualifier turns on the glob_dots option for this pattern). Jan 28, 2011 at 20:14
  • Or rm -rf {.,}* (unlike bash, zsh doesn't include . and .. into {.,}*, at least on my machine).
    – sasha
    Jan 11, 2017 at 11:50
1

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

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

3
  • 4
    Not in any shell that I know of. Jan 27, 2011 at 22:45
  • you are correct. it does not deletes sub-directories.
    – gladimdim
    Jan 28, 2011 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). Jan 29, 2011 at 3:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .