fnmatch(3)

โœจ fnmatch โ€” bestie edition โœจ

okay so real talk, this is from the Library Functions Manual โ€” it's fnmatch(3) and she is THAT girl of shell wildcard matching. let's get into it ๐Ÿ’…


๐ŸŒธ NAME ๐ŸŒธ

fnmatch โ€” match filename or pathname.

no cap, that's the whole job. she looks at a string and tells you if it vibes with a pattern. ๐Ÿฆ‹


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library: libc, -lc. your fave, literally always there for you ๐Ÿซถ


โœจ SYNOPSIS โœจ

#include <fnmatch.h>

int fnmatch(const char *pattern, const char *string, int flags);

three args, zero drama. she's lowkey elegant like that ๐Ÿ˜Œ


๐Ÿ’– DESCRIPTION ๐Ÿ’–

so. the fnmatch() function checks whether the string argument matches the pattern argument โ€” and that pattern is a shell wildcard pattern (peep glob(7) for the deets, iykyk ๐Ÿ‘€).

the flags argument is what changes her behavior, and bestie it is the bitwise OR of zero or more of the following flags. zero. or more. you pick, she's flexible ๐Ÿ’…

  • FNM_NOESCAPE โ€” when this one's set, a backslash is just a regular character doing regular character things, not an escape character. she's retiring, your honor ๐ŸŽ€
  • FNM_PATHNAME โ€” set this and a slash in string can only be matched by a slash in pattern. not by an asterisk (*), not by a question mark (?), and not by a bracket expression ([]) that contains a slash. slashes stay exclusive, periodt ๐Ÿ”’
  • FNM_PERIOD โ€” set this and a leading period in string has to be matched exactly by a period in pattern. and what counts as "leading"? a period is leading if it's the first character in string, or if both FNM_PATHNAME is set and the period immediately follows a slash. dotfiles are picky, respect her boundaries ๐Ÿ™ˆ
  • FNM_FILE_NAME โ€” this is a GNU synonym for FNM_PATHNAME. same girl, different name ๐Ÿ˜ญ
  • FNM_LEADING_DIR โ€” this one's a GNU extension. set it and the pattern counts as matched if it matches an initial segment of string that is followed by a slash. also important: this flag is mainly for the internal use of glibc and is implemented only in certain cases. so like, don't build your whole personality on it ๐Ÿ’€
  • FNM_CASEFOLD / FNM_IGNORECASE (same as FNM_CASEFOLD) โ€” set this and the pattern is matched case-insensitively. no uppercase slander here, every letter is welcome ๐ŸŒˆ
  • FNM_EXTMATCH โ€” a GNU extension. set it and extended patterns are supported โ€” as introduced by 'ksh' and now supported by other shells. the extended format goes like this, where pattern-list is a '|'-separated list of patterns ๐Ÿ‘‘
  • '?(pattern-list)' โ€” the pattern matches if zero or one occurrences of any of the patterns in the pattern-list match the input string.
  • '*(pattern-list)' โ€” the pattern matches if zero or more occurrences of any of the patterns in the pattern-list match the input string.
  • '+(pattern-list)' โ€” the pattern matches if one or more occurrences of any of the patterns in the pattern-list match the input string.
  • '@(pattern-list)' โ€” the pattern matches if exactly one occurrence of any of the patterns in the pattern-list match the input string.
  • '!(pattern-list)' โ€” the pattern matches if the input string cannot be matched with any of the patterns in the pattern-list. the hater of the group, and we love her for it ๐Ÿ’€โœจ

๐ŸŽฏ RETURN VALUE ๐ŸŽฏ

  • Zero if string matches pattern โ€” slay, we love a clean zero ๐Ÿ’š
  • FNM_NOMATCH if there's no match ๐Ÿ˜ญ
  • another nonzero value if there's an error. so likeโ€ฆ not every nonzero is FNM_NOMATCH, check before you spiral fr fr

๐Ÿงต ATTRIBUTES ๐Ÿงต

for an explanation of the terms used in this section, go see attributes(7).

  • Interface: fnmatch()
  • Attribute: Thread safety
  • Value: MT-Safe env locale

(that was a table in the original, bestie โ€” we're just vibing in bullets now ๐ŸŽ€)


๐Ÿ“œ STANDARDS ๐Ÿ“œ

  • fnmatch() โ€” POSIX.1-2008.
  • FNM_CASEFOLD, FNM_IGNORECASE โ€” POSIX.1-2024.
  • FNM_FILE_NAME, FNM_LEADING_DIR โ€” GNU.

๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

  • fnmatch() โ€” POSIX.1-2001, POSIX.2.
  • FNM_CASEFOLD โ€” has been available on many systems even before POSIX.1-2024. she was an icon before the paperwork caught up, iconic behavior ๐Ÿ‘‘

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

sh(1), glob(3), scandir(3), wordexp(3), glob(7)


and that's the whole page, bestie โ€” go match some strings, treat backslashes with respect, and don't forget that FNM_LEADING_DIR is glibc-internal and only implemented in certain cases ๐Ÿ˜ญ๐Ÿ’…

this has been Linux man-pages 6.20, dated 2026-02-08, page fnmatch(3) ๐ŸŒธ stay matching, stay MT-Safe ๐Ÿซถโœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms