I would like to remove all content of the current working directory. How to do it in efficient and elegant way?

I have several files, directories and some of them starts with dot, like:

touch .abc; touch abcd ; mkdir foobar; touch "file name" "#"

I am looking for posix compatible, elegant solution, to make this directory empty. I will post some of my ideas, but none of them are perfect.

  • d=$(basename `pwd`); cd .. && rm -rf ./${d} && mkdir ${d} && cd - – devnull Apr 11 '13 at 12:46
  • 1
    @devnull: fails if you don't have perms to remove CWD. – Michał Šrajer Apr 11 '13 at 13:13
  • It was assumed that you have perms on the directory. Nevertheless, this should work regardless d=$(basename `pwd`); cd .. && mktemp -p ./${d} && rm -rf ./${d} && mkdir ${d} && cd - – devnull Apr 11 '13 at 13:36
  • Delete including subdirs? – user unknown Feb 14 '18 at 19:48
rm -rf -- * .*

This is portable and works, but it's ugly, because except with zsh, it issues warnings:

rm: cannot remove directory: `.'
rm: cannot remove directory: `..'
  • Yeah, you can adapt with stuff like [^.] but then you'll have other problems (like folders named ..abc); so, I doubt you can get rid of these nasty warnings. It's not a "clean" option, but it works. – Tamara Wijsman Apr 11 '13 at 10:07
  • @TomWijsman, [^.] is not POSIX, [!.] is. – Stéphane Chazelas Apr 11 '13 at 10:46
  • @StephaneChazelas: If you need POSIX, then convert the syntax, I don't care... – Tamara Wijsman Apr 11 '13 at 11:04

Try this:

find . -maxdepth 1 -not -name \. -exec rm -rf \{\} ;


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

That one could fail if the list of files it too big, this one wouldn't:

find . ! -name . -prune -exec rm -rf {} +

With zsh:

rm -rf -- *(D)

Best solution I found is to:

find . -delete

however, -delete predicate is risky (see man) and I don't like to use it.


I don't like -delete predicate because it can cause a disaster when used by someone not familiar with the details of find's logic. For example:

find . -delete -name '*.bak'  # DO NOT DO IT!!!

Someone may expect such call to remove only '*.bak' files, but it is exact equivalent to find . -delete.

  • This should be the best option; I don't see why it would be risky, it's logic to put it at the end and you can first run it without that to see what it would delete. – Tamara Wijsman Apr 11 '13 at 10:05
  • @TomWijsman in this case -delete is fine, but in general -delete is risky, this is why I don't like it. – Michał Šrajer Apr 11 '13 at 10:22
  • 1
    -delete is not POSIX. – Stéphane Chazelas Apr 11 '13 at 10:37
  • 1
    What about -exec rm -rf {} \;? – Tamara Wijsman Apr 11 '13 at 11:01

how about

 rm -rf * .[a-z]* .[A-Z]* .[0-9]*

will that work ? or you have other file name patterns to match as well ?

  • in posix filesystem, almost any character can be in the filename. Try touch '#'. – Michał Šrajer Apr 11 '13 at 10:03
  • 1
    Restricting to certain characters isn't really reliable; you're missing the often used . and - for instance, but even including these you may come across a _ or other less frequent characters. – Tamara Wijsman Apr 11 '13 at 10:08

another option is to remember value of pwd, cd .., rm -rf the directory and recreate the directory. This is also ugly for many reasons.

BTW, please do not down-vote. Comment if needed.


First delete files, then empty directories:

find . -type f -exec rm \{\} \;
find . -depth -type d -empty -exec rmdir \{\} \;
  • this "trivial task" is getting crazy... ;-) – Michał Šrajer Apr 11 '13 at 10:28
  • -empty is not POSIX. -type f is for regular files only. You would miss other types of files like symlinks, sockets, pipes, doors, devices... Use ! -type d instead. – Stéphane Chazelas Apr 11 '13 at 10:52
rm -rf .

This of course does not work at all, however this:

rm -rf -- "$PWD"

removes CWD, and we end up within nonexisting directory. This is very ugly.

BTW, please do not down-vote. Comment if needed.

  • Well, this does not remove the "content" of a directory. – Tamara Wijsman Apr 11 '13 at 10:06
  • @TomWijsman. Agree. This is why it would need recreation of the directory. Yet another ugly solution... – Michał Šrajer Apr 11 '13 at 10:09

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.