You'll need to write a script to do this with Python or Perl probably being your best bet.
Here is a script I wrote in Perl that does basically that (jpeg & raw files) and automates moving them to a "dated directory structure" (i.e. YYYY/MM-month/DD). It uses the Image::ExifTool
library to extract the date of the photo to know where to put it.
For your exact question, you can see that it finds all .jpg files, works out the basename, and then checks for a matching .nef file.
#! /usr/bin/perl
$dryrun = 0;
$encode = 1;
use Image::ExifTool;
use Dumpvalue;
my $Dumper = new Dumpvalue();
@Months = qw(00 01-January 02-February 03-March 04-April 05-May 06-June 07-July 08-August 09-September 10-October 11-November 12-December);
$startdir = shift @ARGV;
die "error: no start directory specified\n" unless ($startdir ne "");
foreach $file (split(/\n/,`find "$startdir" -name "*.[Jj][Pp][Gg]" -print | sed -e 's,^\./,,'`)) {
next if ($file =~ m,(^|/).xvpics/,);
print STDERR "$file => ";
my $exif = new Image::ExifTool;
$info = $exif->ImageInfo($file);
if (ref($info) != "HASH") {
print STDERR "error: could not read exif data from '$file' ($@)\n";
next;
}
($filename) = ($file =~ m,([^/]+)$,);
# $Dumper->dumpValue($info);
# next;
# exit(1);
$date = $info->{"CreateDate"};
#print STDERR $date," => ";
unless (($y,$m,$d,$h,$n,$s) = ($date =~ m/^(\d\d\d\d)\D(\d\d)\D(\d\d)\D+(\d\d)\D(\d\d)\D(\d\d)($|\D)/)) {
$date = $info->{"FileModifyDate"};
unless (($y,$m,$d,$h,$n,$s) = ($date =~ m/^(\d\d\d\d)\D(\d\d)\D(\d\d)\D+(\d\d)\D(\d\d)\D(\d\d)($|\D)/)) {
print STDERR "$file: no date for '$file' (skipped)\n";
next;
}
}
next if ($file eq "$outdir/$filename");
system("mkdir","-p",$outdir) unless (-d $outdir || $dryrun);
print STDERR "$outdir/".$filename;
rename($file,"$outdir/".$filename) unless $dryrun;
$jpgfile = $filename;
$file =~ s/\....$/\.nef/;
$filename =~ s/\....$/\.nef/;
if (-f $file) {
print STDERR " ($outdir/$filename)";
rename($file,"$outdir/".$filename) unless $dryrun;
chmod(0644, "$outdir/".$jpgfile) unless $dryrun;
}
print STDERR "\n";
}
This is not high-quality code. :-) It's a hack I wrote for myself but should serve as a reasonable example.