which argument to the -name flag will match files or directories beginning with a ‘.’ (period) ?

With the find command, which argument to the -name flag will match files or directories beginning with a ‘.’ (period) ?

With the find command, which argument to the -name flag will match files or directories beginning with a ‘.’ (period) ?

Answer: [.]*

Explanation:
From the man pages:
-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; see section STANDARDS CONFORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -path. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function.

—————–
This will not work in the unlikely event that a file exists that is actually named "[.]<anything>". So it is always better to enclose the search pattern in quotation marks to prevent the shell from expanding the pattern: find -name ‘.*’



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Chris C

Chris C

Quotes ” “, not single quotes ‘ ‘.

Example from Smith:

find /home -name “*.c”

TT

TT

[] not required for find if single or double quotes are used
it is only required for use with no quotes

Peter

Peter

find /home -name ‘.*’ works also

Peter

Peter

However if the question should be read “which argument… not using quotes” then -name [.]* should be the only solution to this question.