Philipp HB
Philipp HB

Reputation: 179

Moving images based on IPTC metadata in bash

I am trying to reorganise images based on the species that is within an image. Among other information, the species name can be found in the IPTC metadata (see link to the Inspector image). I am attempting to do this in bash on macOS and have tried following code (with template species name and directory):

find . -iname "*.jpg" -print0 | xargs -0 grep -l "Species Name" | xargs -0 -I {} mv {} ~/example/directory

I have also tried using the exiftool package, where the relevant information is in the Subject tag:

find . -iname "*.jpg" -print0 | xargs -0 exiftool -Subject | grep "Species Name" | xargs -0 -I {} mv {} ~/example/directory

However, I receive following error message, which I assume to be a result of a misuse of grep or the last xargs:

mv: rename (standard input) to ~/example/directory(standard input) : No such file or directory

Any ideas what could fix this issue? Thank you in advance.

Upvotes: 1

Views: 625

Answers (2)

StarGeek
StarGeek

Reputation: 5771

Exiftool can move and rename files based upon the file metadata and is much faster calling it once for an entire directory than calling it individually for each file in a loop (Common Mistake #3).

I believe you could use this command to do your sorting:
exiftool -if '$subject=~/Species Name/i' -directory=~/example/directory .

Breakdown:
-if '$subject=~/Species Name/i' Does a regex comparison of the Subject tag for the "Species Name". I added the i at the end to do the comparison case insensitively, modify as desired.
-directory=~/example/directory If the -if condition is met, then the file will be moved to the stated directory.

Upvotes: 4

jjo
jjo

Reputation: 3020

Looks like grep's output newline is interfering there, as you're using a clean -0 pipeline, you should do so with grep -- in Linux it would be grep -Z ... (or --null), dunno if Mac OS's has a similar one.

Upvotes: 1

Related Questions