What sort of regex syntax does -name option of find command expect? I was of the impression that it would be the same as for -regex but that doesn't seem to be the case.

$ mkdir test && cd test
$ touch .sw.
$ touch .swp
$ touch .abc.swo
$ touch notswap.py
$ find . -name "*sw." -type f
$ find . -name "*sw*" -type f
$ find . -regex ".*sw." -type f

FWIW, I am aware that -regex matches the entire path and -name only the base of the filename. I hope it's clear that that's not the issue here. As a more concrete question, how do I match all files ending with .swx where x can be any character using the -name option.

Meta info:

$ find --version
find (GNU findutils) 4.5.11
$ echo $0
-name pattern
              Base of file name (the path with the leading directories
              removed) matches shell pattern pattern.  Because the leading
              directories are removed, the file names considered for a match
              with -name will never include a slash, so `-name a/b' will
              never match anything (you probably need to use -path instead).
              A warning is issued if you try to do this, unless the
              environment variable POSIXLY_CORRECT is set.  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.  Don't forget to enclose the pattern in
              quotes in order to protect it from expansion by the shell.

It uses shell patterns not regex.

Source: find(1)

From the GNU manual under name:

Here are ways to search for files whose name matches a certain pattern. See Shell Pattern Matching, for a description of the pattern arguments to these tests.

2.1.4 Shell Pattern Matching

find and locate can compare file names, or parts of file names, to shell patterns. A shell pattern is a string that may contain the following special characters, which are known as wildcards or metacharacters.

You must quote patterns that contain metacharacters to prevent the shell from expanding them itself. Double and single quotes both work; so does escaping with a backslash.

  • *
    Matches any zero or more characters.
  • ?
    Matches any one character.
  • [string]
    Matches exactly one character that is a member of the string string. This is called a character class. As a shorthand, string may contain ranges, which consist of two characters with a dash between them. For example, the class ‘[a-z0-9_]’ matches a lowercase letter, a number, or an underscore. You can negate a class by placing a ‘!’ or ‘^’ immediately after the opening bracket. Thus, ‘[^A-Z@]’ matches any character except an uppercase letter or an at sign.
  • \
    Removes the special meaning of the character that follows it. This works even in character classes.
    In the find tests that do shell pattern matching (‘-name’, ‘-wholename’, etc.), wildcards in the pattern will match a ‘.’ at the beginning of a file name. This is also the case for locate. Thus, ‘find -name '*macs'’ will match a file named .emacs, as will ‘locate '*macs'’.

Slash characters have no special significance in the shell pattern matching that find and locate do, unlike in the shell, in which wildcards do not match them. Therefore, a pattern ‘foobar’ can match a file name ‘foo3/bar’, and a pattern ‘./srsc’ can match a file name ‘./src/misc’.

If you want to locate some files with the ‘locate’ command but don't need to see the full list you can use the ‘--limit’ option to see just a small number of results, or the ‘--count’ option to display only the total number of matches.

To answer your question:

find . -name "*.sw?" -type f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.