Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

The obvious solution produces an exit code of 1:

bash$ rm -rf .*
rm: cannot remove directory `.'
rm: cannot remove directory `..'
bash$ echo $?

One possible solution will skip the "." and ".." directories but will only delete files whose names are longer than 3 characters:

bash$ rm -f .??*
share|improve this question
Well if you're not too worried about not being able to remove . & .. then who cares? unless you're worried about ugly output in a script then I think the obvious solution is less typing that the others quite frankly. –  Matt Jul 30 '09 at 9:31
Thanks for the comment Matt. I often use the command in scripts with per command exit code checking (set -e). In these cases an indicative exit code is necessary. –  Yonatan Broza Jul 31 '09 at 10:20
And there's always rm -rf .* || true if you just want to get around the set -e behavior for that one statement. –  Domingo Ignacio Galdos Jun 4 '12 at 2:35

3 Answers 3

up vote 17 down vote accepted
rm -rf .[^.] .??*

Should catch all cases. The .??* will only match 3+ character filenames (as explained in previous answer), the .[^.] will catch any two character entries (other than ..).

share|improve this answer
Thanks for the answer! Based on it I also got to the shorter version of "rm -rf .[^.]*". –  Yonatan Broza Aug 2 '09 at 19:00
Be careful with that shorter version, it will give similar, but not identical results. It won't match names with two dots at the front (e.g. ".../" which is sometimes seen hiding rootkits, etc.) –  Russell Heilling Aug 3 '09 at 12:29
And be careful to make a PWD check or u loose all your settings :) –  kommradHomer Dec 11 '13 at 9:46

Just so you know, .. and . are not files. They are references to directories. . (just a single dot) is the current directory, and .. (two dots) is a link to the directory one level up. For example, if cd /home/user, . is equal to /home/user and .. is /home/

In other words, you can't delete the . and ..(.?)

share|improve this answer
There is nothing inherent in . and .. that protects them from deletion with rm -rf. This is just a protection mechanism added in modern variations of rm. –  kubanczyk Dec 5 '09 at 21:46

best way probably is:

  • find . -iname .* -maxdepth 1 -type f -exec rm {} \;

change rm to ls -l if you just want to see what would be deleted, to verbose the output u may want to add -v option to rm

  • -type f options tells find command to look only for files (omit dirs, links etc)
  • -maxdepth 1 tells find not to go down to subdirectories

ps. don't forget about ending '\;'

share|improve this answer
Careful! You mean -iname '.*' or -iname .* or else you will drag in . and .. again anyway. –  Domingo Ignacio Galdos Jun 4 '12 at 2:33

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.