How to delete all files in a current directory starting with a dot?
When you want to delete all dot files, the convention is to use this command:
rm .??*
This will delete all files starting with a dot containing at least two other characters, thus leaving .
and ..
intact. Granted, it will also miss file names with only one letter after the dot, but these should be rare.
.*
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
find /path/to/dir -type f -name ".*" -delete