1

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.

Philipp HB
  • 169
  • 1
  • 14

2 Answers2

4

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.

StarGeek
  • 4,948
  • 2
  • 19
  • 30
  • 1
    After running the suggested code, I get following message `1 directories scanned; 503 files failed condition; 0 image files read` and running the code with the -v2 flag indicates that there is a syntax error. I have tried other options like `exiftool -if "${Keywords//[[:blank:]]/} =~ \W*MalayanPorcupine\W*\i" -directory=~/Desktop/image_data/malayan_porcupine .` but this doesn’t work as well. Any ideas? – Philipp HB May 21 '18 at 21:26
  • Sorry, my mistake. Change the double quotes to single quotes to avoid bash interpreting the dollar sign as the beginning of a shell variable. Also, check which tag you actually want to use. You mention `Subject` in your original post, which is where keywords would be located in XMP (IPTC Core) but `Keywords` in your comment (IPTC Legacy). – StarGeek May 22 '18 at 02:06
  • Works like a charm! Thank you. Worth to mention that I've added the `-r` flag and changed `.` for a directory, as this will have to process files in subdirectories. – Philipp HB May 22 '18 at 08:29
1

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.

jjo
  • 2,595
  • 1
  • 8
  • 16
  • 1
    Thanks for the reply! Unfortunately this hasn't helped. However what I have noticed is that `grep` does not return any pathways even with the -l and/or -Z flag. Instead it returns `(standard input)`. Any suggestions? – Philipp HB May 21 '18 at 15:53
  • Which grep? The one in your first command should print each file name as it's being passed a list of file names by xargs, the one in your second command should print "standard input" since it's getting it's input from a pipe. – Ed Morton May 22 '18 at 00:58