I have a list of filnames like this:


How to best find them all by single query in Spotlight, Terminal, mdfind or locate?

  • Are they all in the same directory (folder)? – thrig Jul 5 '17 at 21:33

I'm going to assume your list is just a representative list and the filenames follow that pattern and may actually be more then those exact filenames. So, the use of an extended regular expression is going to be used to find the files matching the pattern shown in the example filenames in your OP.

find -E . -regex '.*\/P[0-9]{7}\.JPG'
  • -E Interpret regular expressions followed by -regex and -iregex primaries as extended (modern) regular expressions rather than basic regular expressions (BRE’s). The re_format(7) manual page fully describes both formats.
  • . Searches the current directory and its subdirectories.
  • -regex pattern True if the whole path of the file matches pattern using regular expression.
  • .*\/P[0-9]{7}\.JPG
    • .* matches any character (except for line terminators).
    • * Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy).
    • \/ matches the character / literally (case sensitive).
    • P matches the character P literally (case sensitive).
    • [0-9] match a single character present in the list.
    • {7} Quantifier — Matches exactly 7 times, 0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive).
    • \. matches the character . literally (case sensitive).
    • JPG matches the characters JPG literally (case sensitive).

$ find -E . -regex '.*\/P[0-9]{7}\.JPG'

Note: The . is the current working directory, which by default if you just opened Terminal will be your Home directory. If the files are elsewhere you can use a different starting path, e.g. / will start in the root of the Macintosh HD, (assuming default naming). As you can see in the example output the target filenames found by the regex pattern were in my Pictures folder.


Try this:

find . -name 'P1020804.JPG' -o -name 'P1020805.JPG' -o -name 'P1020806.JPG'

Remember that . is necessary.

find . \( -name P1020804.JPG -o \
          -name P1020805.JPG -o \
          -name P1020806.JPG -o \
          -name P1020807.JPG -o \
          -name P1020808.JPG \)

find in the current directory- . and sub-directories, the file name or the next file name... and so on.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .