Where does the pattern occur in a match by find

find

The manpage of find says:

-name

Base of file name (the path with the leading directories
removed)

$ find ~ -name bookmark | less

returns nothing, while

$ find ~ -name bookmarks | less
./.streamtuner/cache/bookmarks
./.config/zathura/bookmarks
./.elinks/bookmarks

has three matches.

Why does the first not have the matches of the second?

If I would like to find those files whose names contain bookmark regardless the position of bookmark in the filename, how should I use find? Thanks.

Best Answer

The pattern given to -name has to match the entire base filename. The behaviour of the -name pattern is defined as:

The primary shall evaluate as true if the basename of the current pathname matches pattern

This means it's true when the whole of the basename matches the pattern you gave. You can think of a pattern as being basically like a shell glob: you can use *, ?, and [...] patterns inside it, with the start and end of the pattern aligned with the start and end of the string.

So your command:

find ~ -name bookmarks

finds files named "bookmarks" because that is the entire filename, but:

find ~ -name bookmark

would only find files named 'bookmark', because there are no wildcard characters in the pattern.

To match files called both bookmark and bookmarks, you could use:

find ~ -name 'bookmark*'

So if you want to find

those files whose names contain bookmark regardless the position of bookmark in the filename

you can use use:

find ~ -name '*bookmark*'

to match files whose names have any number of characters, then bookmark, then any number of characters.

Related Question