That's to work around a bug/misfeature in shells other than zsh
, fish
and the descendants of the Forsyth shell (including pdksh
and derivatives)¹, whereby the expansion of the .*
glob includes .
and ..
(on systems (most, unfortunately) where readdir()
returns them)
With those shells,
chmod -R og-rwx .*
for instance would recursively remove rwx permissions to the current (.
) and parent (..
) directories instead of just the hidden files and directories in the current directory.
It's particularly bad for commands that do things recursively or act on directories like ls .*
, chown -R .*
, find .*
, grep -r blah .*
but it's still annoying for most other commands and I can't think of any commands for which you'd want to have those .
and ..
included in the list of files passed to them.
A safeguard had to be added to the rm
utility to work around that misfeature as too many people were tripping on rm -rf .*
.
With *
added, it's also used to pass all files (hidden or not) as arguments to a command (cmd -- .[!.]* ..?* *
), for which you'll find other workarounds depending on the shell.
The .[^.]*
glob (.[!.]*
in Bourne/POSIX shells) excludes .
(as it matches on filenames with at least two characters) and ..
(as the second character is .
which doesn't match [^.]
), but also excludes files like ..foo
, for which you need the second glob ..?*
.
Those .
and ..
are tools for directory traversal, it's a mistake that they should be listed like ordinary files. POSIX requires them to be understood in path components (like in open(".")
, stat("foo/../bar")
) but not necessarily be implemented as directory entries nor included in readdir()
.
Still, most systems still do implement those like in the early Unices as hard links, and most of those that don't will still fake entries for them in the output of getdents()
/readdir()
.
With bash
, an alternative is to turn the dotglob
option on and use:
chmod -R og-rwx [.]*
(though beware that if there's no non-hidden file, it could change the permissions of the [.]*
file unless you had the failglob
option on to mimic the behaviour of zsh
/fish
).
As a history note, filenames starting with .
being hidden files were born from a coding mistake from someone trying to skip .
and ..
in the first place. It's ironical that when trying to do things with hidden files we would run into the same problem.
¹ see also the globskipdots
option in bash 5.2+
.??*
would not match the file.X
--.??*
matches a dot, then any character then another character then zero or more characters