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 ..?

12 Answers 12


* 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 -rf -- ..?* .[!.]* *

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 -rf -- {} +

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.

  • 14
    This should be the accepted answer, as it prevents parent traversal and possible deletion.
    – rbellamy
    Commented Jun 12, 2015 at 17:06
  • The find alternative returns "success" even if some file is not successfully deleted; not good for script. Commented Jul 29, 2016 at 7:22
  • 3
    @haylem In zsh, you need to write .[^.]* instead of .[!.]* when history substitution is enabled (which by default is the case interactively but not in scripts), because zsh parses ! as a history reference. But in zsh you wouldn't need that in the first place, you can just use *(D) to include dot files (without . or ..) in the wildcard match. Commented Aug 1, 2017 at 14:29
  • 2
    rm -rf /path/to/subdirectory/{..?*,.[!.]*,*} 2>/dev/null
    – bjd2385
    Commented Jun 6, 2018 at 7:00
  • 1
    Also worth noting that using ./* or -- * would be safer in case a filename starts with a dash (-) so that it doesn't get interpreted as an option.
    – Paul P
    Commented Mar 30, 2023 at 9:42

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.

  • 4
    But what if I only want to delete the hidden files? Commented Jun 1, 2014 at 13:52
  • 1
    @CMCDragonkai that should be a different question, but you can find the solution in Gilles' answer (rm ..?* .[!.]* should do it).
    – evilsoup
    Commented Jun 1, 2014 at 18:14
  • 42
    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
    Commented Sep 23, 2015 at 8:46
  • 4
    @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
    Commented Dec 29, 2015 at 5:43
  • 6
    Deleting the directory is not always possible. In my case the directory is a docker volume, and while i can do whatever I want inside the volume, i cant change the volume itself from within the container.
    – dovidweisz
    Commented Jun 3, 2019 at 15:15

Just realised this is the most convenient way in most Linux distros:

ls -A1 | xargs rm -rf


-A = list everything except . and ..

-1 = put every item in one line

  • 4
    downside which I see is: you need to 'cd' into directory to do this.
    – kumar
    Commented Nov 24, 2016 at 9:38
  • ls -A1 /your/path/ | xargs rm -rf should apply Commented Nov 24, 2016 at 14:02
  • nope..it does not help - For example: ls -A1 ~/test/ | xargs rm -rf this command deletes files which I am currently 'cd into'..not in the directory ~/test.
    – kumar
    Commented Nov 25, 2016 at 6:44
  • 14
    It doesn't work if any of the file names contain blanks, newlines, quotes or backslashes. Commented Jun 13, 2017 at 12:14
  • 1
    If there is blank inside filename, it will be split into multiple file names. So this command is better ls -A1 | xargs -I{} rm -rf {}
    – Qi Luo
    Commented Feb 9, 2022 at 6:44

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

find somedir -mindepth 1 -delete
  • 4
    Or you could simply rm -rf /some/dir and then create a new empty directory in its place.
    – tripleee
    Commented May 26, 2013 at 12:11
  • @tripleee, in such case permissions might differ in the result.
    – Artfaith
    Commented Dec 18, 2021 at 21:16
  • Sure, there are scenarios where you can't recreate it exactly if it didn't belong to you in the first place even though you had write permissions. But in most real-life scenarios all you need is check the permissions so you can set them back to what they were.
    – tripleee
    Commented Dec 19, 2021 at 12:04
  • Any downsides to this? Curious as to why this is not the top answer...
    – Xen
    Commented Aug 26, 2022 at 15:53

This should work just like @Gilles answer but more compact:

rm -rf {,.[!.],..?}*


rm -rf dir/to/files/{,.[!.],..?}*

should also add an if for usage in scripts just to be safe:

if [ -d "$DIR" ]; then
    rm -rf ${DIR}/{,.[!.],..?}*
  • 1
    Oddly enough, as a bash alias on Ubuntu 16.04 LTS, the previous answer wasn't working. However, alias cleandir='rm -rf {,.[!.],..?}*' does. Commented Apr 14, 2018 at 18:44
  • @StevenVentimiglia Interactions with ! and history? What shell? Commented Mar 8, 2020 at 19:52
  • Flippin heck - NO. Do not ever write rm -rf $x/..... It $x evaluates to empty you will delete a whole lot more than you want to. And believe me, and some point, now or later, it WILL be empty. just find another way - do a cd to $DIR like: `if cd $DIR # NO TRAILING SLASH then if [ "/" != "$(pwd)" ] rm -rf ./ Commented Jan 10, 2022 at 18:11

I suggest you experiment with

Turn-ON dots (hidden files)

  • set dotglob

    shopt -s dotglob

Turn-OFF dots

  • unset dotglob

    shopt -u dotglob

This method worked exactly as I wished for a copy command that was missing the hidden directories.

    shopt -s    dotglob
    cp    -rvn  ./$from/*  ./$too/
    shopt -u    dotglob

So I did a remove (delete), and oops ...

    shopt -s    dotglob
    rm -fr ../message_splitter--044a/*
    shopt -u    dotglob

... that works too!

It occurs to me that you dear reader can't see the message_splitter directory. Any way it has a .svn folder that needs to be removed, and copied Into.

From man page ...

dotglob If set, bash includes filenames beginning with a `.' in the results of pathname expansion.


  • Behaviour of shopt is unspecified. This answer may be useful for a subset of shells, but you didn't specify which ones. Commented Apr 7, 2023 at 12:07

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 ..?

Assuming the directory in question is ./dir, then

rm -rf ./dir

would remove all files in ./dir, including hidden files and directories, recursively, and including the ./dir directory itself.

If you do not want to delete the directory itself, then you may just recreate it afterwards, or use

find ./dir -mindepth 1 -delete

or if you find does not support -delete,

find ./dir -mindepth 1 -depth -exec rm -rf {} ';'

Using -mindepth 1 allows you to keep the top-level directory ./dir.

  • Note, if the directory itself is the mount point for a filesystem, eg /mnt/usbmemorystick (if your goal is to delete all the files on a filesystem), then you can't just delete that directory itself. Commented Nov 2, 2022 at 1:55
  • @CraigMcQueen In that case the speediest alternative may be to unmount the file system, recreate it, and mount it again.
    – Kusalananda
    Commented Nov 2, 2022 at 4:22
  • Possibly, although that comes with its own set of challenges, such as making sure to recreate the filesystem with the same parameters. Some filesystems have some subtle parameters. Eg for ext4, the inode size, with consequences for Y2038 compatibility. Commented Nov 3, 2022 at 3:11
  • The rm suggestion also breaks if dir is a symlink, or if the directory has non-default permissions, ACLs, etc. Commented Mar 26 at 23:26
  • @ChrisDavies Are you referring to rm -rf ./dir? I thought it was given that we were talking about a directory. Also, the permissions on the parent directory of dir may come into play, that's true, but if dir disallowed removal of contents, then you would obviously need to chmod it to be able to remove anything inside it (the question indicates that this is no issue). The only issue with not being allowed to remove dir itself is that you don't have to recreate it later :-P I'm not a Linux user, so I'll leave the ACL, SELinux and other weirdness to you guys :-)
    – Kusalananda
    Commented Mar 26 at 23:54

Why nobody mentions:

rm -rf * .*
  • 4
    Because this gives the same warnings that the user in the question wants to avoid.
    – Kusalananda
    Commented Apr 10, 2020 at 8:37
  • Oh my bad. But this is the shortest one line to get the thing done.
    – Qian Chen
    Commented Apr 10, 2020 at 18:32
  • Indeed, except that it doesn't answer the question as asked Commented Mar 26 at 23:25

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.

  • to delete all directories in the current directory you could do find . ! -name '.' ! -name '..' -type d -delete
    – Andy
    Commented Jul 3, 2016 at 4:01

What about using find with both -maxdepth and -mindepth? This can be also run outside the directory you want to clear.

find target/ -maxdepth 1 -mindepth 1 -exec rm -fr -- {} +;

Here's a complete example:

mkdir -p target/{plain{0..3},.hidden{0..3}}; 
touch target/{plain{0..3},.hidden{0..3}} target/{plain{0..3},.hidden{0..3}}/{plain{0..3},.hidden{0..3}};
tree -a;
find target/ -maxdepth 1 -mindepth 1 -exec rm -fr -- {} +;
tree -a;

Shorter version

rm -r * .*

rm: remove

-r: recursively

*: all directories

.*: all hidden directories ( ex: .git, .gitignore )

use an optional force flag ( --force or -f ). Check the usage here.

rm -rf * .*

Note: ( for zsh users )
The above command doesn't work partially i.e If it can't find files/ directories matching any of * or .* the other one won't execute.

You can fix this using the command

setopt no_nomatch

zsh commands are scoped to a session(/tab). ( if you open a new session you will still face the issue ).

add this line to .zshrc file to apply for all sessions.

ps: restart iterm after making changes to .zshrc

  • You forgot to explain how this avoids the warnings for . and ... Commented Apr 7, 2023 at 12:03
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 7, 2023 at 17:16
  • I used the command recently, it didn't give any warning that was mentioned in the question. Should I add this? Commented Apr 8, 2023 at 6:15

the find command of the other answer did not work for me as it tries to remove the directory itself. I found this one works better:

remove() {
  find "$1" ! -name "$1" -a -prune -exec rm -rfv -- {} +

remove foobar

Note, you must not include a / at the end of the directory, otherwise it will try to remove the directory as well.

You can use -wholename instead of -name if your path starts with /.

The v of the rm command prints out the files as they are being deleted.

You must log in to answer this question.

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