Find multiple files at once via Terminal
I have a list of filnames like this:
P1020804.JPG
P1020805.JPG
P1020806.JPG
P1020807.JPG
P1020808.JPG
How to best find them all by single query in Spotlight, Terminal, mdfind or locate?
Solution 1:
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). There_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 between0
(index 48) and9
(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'
./Pictures/P1020799.JPG
./Pictures/P1020800.JPG
./Pictures/P1020801.JPG
./Pictures/P1020802.JPG
./Pictures/P1020803.JPG
./Pictures/P1020804.JPG
./Pictures/P1020805.JPG
./Pictures/P1020806.JPG
./Pictures/P1020807.JPG
./Pictures/P1020808.JPG
./Pictures/P1020809.JPG
./Pictures/P1020810.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.
Solution 2:
Try this:
find . -name 'P1020804.JPG' -o -name 'P1020805.JPG' -o -name 'P1020806.JPG'
Remember that .
is necessary.
Solution 3:
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.