Bash – the difference between [.]* and ‘.*’ for command ‘find -name’

bashfindlinux

My goal is to find all files or directories in a filesystem that start with a dot (.), for example .gnupg. So I came up with the command:

find -name '.*'

Checking on the Internet, I saw some hints saying that I should use [.]* instead as the parameter value for -name option. What is the difference between both approach, since the result looks like the same?

Best Answer

There's no difference at all. If you try:

$ diff <(find -name '.*') <(find -name '[.]*')

you get no output. This means that command outputs are identical. I think you're taking that "hint" out of context. The intent was probably something different.

From the find man page:

-name pattern
Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters (*',?', and []') match a.' at the start of the base name (this is a change in findutils-4.2.2)

meaning that [] in the -name argument is construed as a character class. With nothing but . in it, it matches a literal . as if you didn't use the [].

Related Question