find
doesn't seem to have such capabilities built in, but you can use xargs
to construct multiple find
commands using arguments from a file, like:
xargs -a patterns.txt -I% find Pictures/ -name %
where patterns.txt
would be a list of patterns suitable for the -name
filter, one pattern per line. Pay attention that you don't have leading/trailing spaces in there, as they would be included in the pattern. An example:
*.jpg
2018-06-*
*foo*
unicorn.png
Note: While this answer looks quite easy and elegant, it got correctly pointed out in the comments that it has a few downsides:
Performance is not too great for large folders or many patterns, as it will run find
once per pattern in your file, causing it to repeatedly scan the whole search folder.
Because of that, also if you have multiple patterns which could potentially overlap (like *.jpg
and *foo*
), files matching more than one pattern will appear that many times in the result. If you're only printing the names anyway, you could pipe the output through sort -u
to remove duplicates, but if you e.g. remove those results or run any -exec
commands on them, this may be more undesirable.
If any of these downsides are a problem for your use case, maybe better go for one of the alternative answers.
Explanation of the command:
xargs
will read a list of arguments and use them to construct and run a new command line.
-a patterns.txt
tells it to read from that file instead of the standard input.
-I%
tells it to not simply append the arguments it read to the end of the command line, but to replace the %
character in the command line you supplied with one argument. This implies creating and running one separate command per input argument.
find Pictures/ -name %
is the command line into which we want to insert the arguments, replacing the %
. You don't need quoting here, because xargs
will take care that each argument it inserts will be treated as single token and not get split on its own. You can of course replace the Pictures/
with your own search directory and also use a different and/or more filters other than just -name
. Because we use the insert option, you can also append actions like -exec ...
to the end of the command.
find /somewhere -name 'PATTERN'
, you want to read multiple PATTERNs from a file, line by line? – Byte Commander Jun 14 '18 at 07:32