How do I delete everything in a directory, including hidden files and directories?
Right now, I use the following:
rm -rf *
rm -rf .*
How do I delete everything in a directory, including hidden files and directories?
Right now, I use the following:
rm -rf *
rm -rf .*
Simple and straight forward:
find -delete
Includes directories and hidden files. At least gnu-find knows -delete
, your find
may differ.
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.
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 ..
.
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
.
if you are in the directory:
cd .. && rm -rf dir && mkdir dir && cd dir
otherwise:
rm -rf /path/to/dir && mkdir /path/to/dir
Oh my Zsh
rm -rf (.|)*
Again, this is for Zsh only.
*(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
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 {} \;
Try rm -rf *?*
. This will delete normal and hidden files.
*?*
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 03:21
rm -rf .* *
. – user unknown Mar 21 '11 at 03:38rm -rf yourdirectory/*
– shreyansp Apr 05 '16 at 09:08..
directory, which will delete whatever is in the directory above. – Richard Peterson Dec 28 '16 at 18:56