Project

General

Profile

Execute exiv2 on specific directory

Added by Peter Bockenhauer over 6 years ago

I am trying to execute exiv2 from within a shell script (OS X). I want to execute it on a specific directory of photos. What is the correct syntax to point it at a directory?

exiv2 r'%Y%m-%d_%H%M_:basename:' rename $(ls)


Replies (7)

RE: Execute exiv2 on specific directory - Added by Robin Mills over 6 years ago

Peter

The examples in our man page are:

exiv2 rename img_1234.jpg
Renames img_1234.jpg (taken on 13−Nov−05 at 22:58:31) to 20051113_225831.jpg

exiv2 −r’:basename:_%Y%m’ rename img_1234.jpg
Renames img_1234.jpg to img_1234_200511.jpg
I'll be honest and admit, I've never used the exiv2 rename command.

In bash (on MacOS-X or Cygwin or Linux) when I want to operate on a lot of files, I like to use a for and find. Something like this. For a single directory:

for i in *.jpg; do ls -alt $i; done
For a tree of files, I usually use find:
for i in $(find . -name "*.jpg"); do ls -alt $i; done
Another couple of useful command are args:
find . -name "*.jpg" | args ls -alt 
If I think there's a possibility of spaces in directory or filename, I'd use the find/zargs arguments -print0 | xargs -0. For example:
find . -print0 -name "*.jpg" | xargs -0 ls -alt 
However, I encourage you to proceed with caution. You don't want to mess up all your files in an instant. I highly encourage you to use 'echo' to see what command is likely to be executed. For example:
find . -name "*.jpg" | xargs echo ls -alt
And very even greater comfort, I have a little program args.cpp:
#include <stdio.h>
int main(int argc, char* argv[])
{
    int i = 1 ;
    while ( i < argc ) {
        printf("%-2d: %s\n",i,argv[i]) ;
        i++;
    }
    return 0 ;
}
784 rmills@rmillsmbp:~/temp/forPeter $
Here's an example:
790 rmills@rmillsmbp:~/temp/forPeter $ ls -alt
total 16
drwxr-xr-x+   4 rmills  staff   136  7 May 17:23 .
-rw-r--r--+   1 rmills  staff     4  7 May 17:23 I am a different file.jpg
-rw-r--r--+   1 rmills  staff     4  7 May 17:22 I am a file.jpg
drwxr-xr-x+ 105 rmills  staff  3570  7 May 17:22 ..
791 rmills@rmillsmbp:~/temp/forPeter $ find . -type f -print0 -name "*.jpg" | xargs -0 args ls -alt
1 : ls
2 : -alt
3 : ./I am a different file.jpg
4 : ./I am a file.jpg
792 rmills@rmillsmbp:~/temp/forPeter $ 

RE: Execute exiv2 on specific directory - Added by Peter Bockenhauer over 6 years ago

Thanks. I'm guessing that exiv2 cannot specify a directory.

I first tried entering the directory and then running the exiv command and that works fine if I execute my shell script manually. But when I put it into cron, it doesn't execute.

The Temp folder below holds the images I am processing the file renames on. I want to have exiv2 rename all the JPGs in that folder.

#!/bin/sh
cd /Volumes/Macintosh\ HD\ 2/Pictures/Temp
exiv2 r'%Y%m-%d_%H%M_:basename:' rename $(ls)

RE: Execute exiv2 on specific directory - Added by Peter Bockenhauer over 6 years ago

After some research, I believe I cannot enter a directory "cd" like this and run the exiv2 command because it doesn't know it's in that directory (from cron at least). I can't figure out how to tell it to run the exiv2 command inside this Temp directory.

RE: Execute exiv2 on specific directory - Added by Robin Mills over 6 years ago

I always sigh when I have to work with cron. I know it always takes time to get things to work correctly with cron. I think cron might use a different user account and it might not be running bash! And if it's running bash, it might be using shell options to disable globing and other horrors. Some coffee and a little determination are needed to tame the beast.

The syntax of exiv2 is:

exiv2 [options] [action] file ...
Doesn't mention a directory. You feed him files.

Incidentally, I believe that Apple prefer user to use launch services to run occasional jobs or daemons. However I run MacOS-X and have a couple of cron scripts that have been running for years without trouble.

RE: Execute exiv2 on specific directory - Added by Peter Bockenhauer over 6 years ago

Yup, I believe I lose many hairs dealing with cron. Yes, you are right, Apple wants us to use launchd, I forgot that. I'll look into doing that instead, maybe I'll have better luck.

Besides that, exiv2 is exactly what I was looking for and it seems to do the job perfect for me! Thanks.

RE: Execute exiv2 on specific directory - Added by Robin Mills over 6 years ago

Whoops, our messages have overlapped. Well, you're not using bash. build and install exiv2 and he should be on your path. Try this (and the coffee):

#!/bin/bash
find '/Volumes/Macintosh\ HD\ 2/Pictures/Temp' -type f -print0 | xargs -0 exiv2 r'%Y%m-%d_%H%M_:basename:' rename 
Spaces in directory and filenames are very troublesome creatures.

RE: Execute exiv2 on specific directory - Added by Peter Bockenhauer over 6 years ago

I got it working! Cron didn't know where exiv2 was located, so I had to give it the full path:

cd ~/Pictures/Temp
/usr/local/Cellar/exiv2/0.24/bin/exiv2 r'%Y%m-%d_%H%M_:basename:' rename $(ls)

    (1-7/7)