J Doe
J Doe

Reputation: 113

Find Command with multiple file extensions

I'm looking through many sub directories and finding all the files ending in .JPG .jpg and .png and copying them to a separate directory, however just now its only finding .JPG

Could someone explain what i'm doing wrong?

find /root/TEST/Images -name  '*.png' -o  -name '*.jpg' -o -name '*.JPG' -exec cp -t /root/TEST/CopiedImages {} +

Upvotes: 0

Views: 2645

Answers (1)

randomir
randomir

Reputation: 18697

You have to group the -o conditions because -a, the implied AND between the last -name '*.JPG' and -exec has higher precedence:

find /root/TEST/Images \( -name  '*.png' -o  -name '*.jpg' -o -name '*.JPG' \) -exec cp -t /root/TEST/CopiedImages {} +

Grouping is done with parentheses, but they have to be escaped (or quoted) due to their special meaning is shell.

Unrelated to this, you can shorten the overall expression by combining filters for jpg and JPG with the case-insensitive -iname (as noted in comments):

find /root/TEST/Images \( -name  '*.png' -o  -iname '*.jpg' \) -exec cp -t /root/TEST/CopiedImages {} +

Upvotes: 4

Related Questions