Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

rm -rf /some/path/* deletes all non-hidden files in that dir (and subdirs).

rm -rf /some/path/.* deletes all hidden files in that dir (but not subdirs) and also gives the following error/warning:

rm: cannot remove directory: `/some/dir/.'
rm: cannot remove directory: `/some/dir/..'

What is the proper way to remove all hidden and non-hidden files and folders recursively in a target directory without receiving the warning/error about . and ..?

share|improve this question
up vote 20 down vote accepted

You could always send error messages to /dev/null

rm -rf /some/path/.* 2> /dev/null

You could also just

rm -rf /some/path/
mkdir /some/path/

...then you won't have to bother with hidden files in the first place.

share|improve this answer
Deleting and remaking the directory seems to be the most straightforward approach. Thanks! – Jake Wilson May 28 '13 at 4:13
But what if I only want to delete the hidden files? – CMCDragonkai Jun 1 '14 at 13:52
@CMCDragonkai that should be a different question, but you can find the solution in Gilles' answer (rm ..?* .[!.]* should do it). – evilsoup Jun 1 '14 at 18:14
Doesn't deleting and recreating the directory pose the risk that the file permissions are not right afterwards (especially important in server environments). Who could one create the folder with the same permissions as before automatically? – Yo Ludke Sep 23 '15 at 8:46
@YoLudke You are absolutely correct, in many situations it doesn't matter but deleting the folder and recreating is not semantically equivalent to emptying that folder; so be careful doing that! – Thomas Dec 29 '15 at 5:43

* matches all non-dot-files, .[!.]* matches all dot files except . and files whose name begins with .., and ..?* matches all dot-dot files except ... Together they match all files other than . and ... If any of these three patterns matches nothing, it expands to itself; rm -f doesn't care about non-existent arguments, so this doesn't matter.

rm -f ..?* .[!.]* *

You can also use find. This is more complex but has the advantage of working even if there are so many files that the wildcards above would expand beyond your system's command line length limit.

find . -name . -o -prune -exec rm -- {} +

If your find has -delete or -mindepth and -maxdepth, you can shorten this to

find . -name . -o -prune -delete
find . -mindepth 1 -maxdepth 1 -delete

You may find it clearer to remove and recreate the directory. This has the advantage (or downside, as the case may be) of resulting in an empty directory even if another program is concurrently creating files in the original directory.

share|improve this answer
This should be the accepted answer, as it prevents parent traversal and possible deletion. – rbellamy Jun 12 '15 at 17:06

Either change the dotglob option of your shell and use *, or use something like find.

find somedir -mindepth 1 -delete
share|improve this answer
Or you could simply rm -rf /some/dir and then create a new empty directory in its place. – tripleee May 26 '13 at 12:11

Find is your friend.

find ! -name '.' ! -name '..' -delete

% find ! -name '.' ! -name '..'
% find ! -name '.' ! -name '..' -delete    
% find ! -name '.' ! -name '..'     

If you wish to use recursively search something other your current directory ($PWD), then add a path right after the find command; e.g., find /path ! -name '.' ! -name '..' -delete. If you only want to descend n number of directories, then use the -maxdepth n option right after the /path parameter.

The above command was tested on an Ubuntu 13.04 system. Will likely work on other, modern linux systems.

share|improve this answer
to delete all directories in the current directory you could do find . ! -name '.' ! -name '..' -type d -delete – Andrew Anthony Gerst Jul 3 at 4:01

Try this code:

Dirlist=``find /some/path/ -type d -name '.[^.]*'`
for HiddenDir in $Dirlist
    rm -rf $HiddenDir
share|improve this answer

Your Answer


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.