Why does find not find my directory neither with -name nor with -regex

find's -name takes a shell/glob/fnmatch() wildcard pattern, not a regular expression.

GNU find's -regex non-standard extension does take a regexp (old style emacs type by default), but that's applied on the full path (like the standard -path which also takes wildcards), not just the file name (and are anchored implicitly)

So to find files of type directory whose name starts with machine, you'd need:

find . -name 'machine*' -type d

Or:

find . -regextype posix-extended -regex '.*/machine[^/]*' -type d

(for that particular regexp, you don't need -regextype posix-extended as the default regexp engine will work as well)

Note that for the first one to match the name of the file also needs to be valid text in the locale, while for the second one, it's the full path that needs to be valid text.


The argument for -name is much like a shell glob, so it's implicitly bound to the start and end of the filename it's matching, and you need * to indicate there may be more to come:

find . -type d -name 'machine*'

For your alternatives, -regex is documented (see man find) as "a match on the whole path, not a search", also implicitly bound to the start and end of the match, so you'd need

find . -type d -regextype posix-extended -regex '(.*/)?machine[^/]*'

When you say that you "tried also with -name", you forgot that -regex requires a parameter, so it used -name as that parameter and then choked on the unexpected '^machine'


The -name test of find, does not take regular expressions, it takes file globs. The ^ has no special meaning in globs, so your command is looking for directories actually named ^machine. You want this, which will find all directories whose name starts with machine:

find . -type d -name 'machine*'

Your other attempt failed because -regex is like -name. It isn't a flag to enable, you need to pass it a regular expression as input. Here you gave it nothing, so find complained. Also, note that the -regex test will try to match the entire path, not just the name. The right way to do what you want using the -regex test would be something like:

find . -maxdepth 1 -type d -regex '.*/machine.*'