The problem consists of the following parts:
- Find all photos under the
Photos-Mod
directory - Find each corresponding photo under
Photos-Orig
- Invoke
exiftools
with correct arguments
Finding all photos
This is easy by using a glob expression like ~/Photo-Mod/*/*
.
Finding the corresponding photo
Given a full path, we can substitute the Photo-Mod
for Photo-Orig
. We further change the file extension to *
so that we have another glob expression. If this glob matches no files, we print an error message, but simply carry on.
Invoking exiftools is now easy.
A Bash solution
Quick recap of Bash syntax:
Foreach-loops:
for variable in EXPR; do COMMANDS; done
If-else:
if COND; then COMMANDS; else COMMANDS; fi
Filetests:
[ -e $file ]
(-e
tests for existence of files)variable assignments:
var=$(COMMANDS)
.
We can use regular expressions via sed
.
# There are probably more elegant ways, but I don't know much bash for mod in ~/Photo-Mod/*/*/*; do orig=$(echo $mod | sed -e 's/Photo-Mod/Photo-Orig/;s/\.[^.]*$/.*/'); orig=($orig); # Apply the glob expression, select result if [ -e $orig ]; then exiftool -overwrite_original_in_place -tagsFromFile "$mod" -gps:all "$orig"; else echo "No corresponding file for $mod found. Skipping!" >&2; fi; done
This can be run directly on the commandline by removing all newlines. You can also put it into a shell script., and invoke it from there.
(Note: tested with similar glob expressions and without exiftools
under GNU bash)
A Perl solution
Quick introduction to Perl syntax:
Foreach-loops:
for my $variable (LIST) { COMMANDS }
If-else:
if (COND) { COMMANDS } else { COMMANDS }
Filetests:
-e $filename
variable assignments:
my $var = EXPR;
Regular expressions are part of the language, and can be applied by $var =~ /REGEX/
.
use strict; use warnings; # helps us write better scripts use autodie; # automatic error handling for my $mod (glob '~/Photo-Mod/*/*/*') { my $orig = $mod; $orig =~ s/Photo-Mod/Photo-Orig/; $orig =~ s/\.[^.]*$/.*/; if (($orig) = glob qq("$orig")) { # implicitly tests for existence system 'exiftool', '-overwrite_original_in_place', '-tagsFromFile', $mod, '-gps:all', $orig; } else { warn "No corresponding file for $mod found. Skipping!\n"; } }
You can dump the code in a file, then invoke it with perl script.pl
. Or you can use a here-doc:
$ perl <<'END' # The code goes here END
(Note: The Perl scripts were tested with perls v16.3 and v14.2 on Linux.)