Shell – How to Delete All Files Starting with a Dot in Current Directory

command linedot-filesshellwildcards

I use Ubuntu 14.04 and in a terminal I became root with sudo su and I wanted to delete root's trash manually. It deleted everything except for a few files that start with a dot. Like .htaccess etc. So I went to that directory (which is "files") and I ran this command:

rm -rf .*

It did delete those files, BUT I also got an error message that the system couldn't delete "." and ".."
What does it mean? Like if I tried to delete the whole directory tree?
Like I said, when I was running that command I was in the lowest directory. This one to be exact: /root/.local/share/Trash/files/
I shot down my PC and then turned it on. Everything seems to be normal at first glance. So now I want to ask is what went wrong and if what I did could really cause any serious damage to the system in general? In other words, should I be worried now or everything is OK?

Best Answer

.* matches all files whose name starts with .. Every directory contains a file called . which refers to the directory itself, and a file called .. which refers to the parent directory. .* includes those files.

Fortunately for you, attempting to remove . or .. fails, so you get a harmless error.

In zsh, .* does not match . or ... In bash, you can set

GLOBIGNORE='.:..:*/.:*/..'

and then * will match all files, including dot files, but excluding . and ...

Alternatively, you can use a wildcard pattern that explicitly excludes . and ..:

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

or

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

Alternatively, use find.

find . -mindepth 1 -delete
Related Question