For each photo my camera produces both a IMG_NNNN.JPG and a IMG_NNNN.CR2 file, that have exactly the same modification time (which, coming from a FAT-formatted SD card, is always an even number of seconds).

I keep JPG and CR2 separately, but I sometimes need to find the CR2 that corresponds to a given JPG. My current scripts use something like:

find /path/to/CR2s -name $(basename $jpgfile .JPG).CR2

But unfortunately due to numbering rollover using the name alone is no longer sufficient. Using the name and the modification time would be perfect, but something like:

find /path/to/CR2s  -name $(basename $jpgfile .JPG).CR2 -newermt  "$(stat -c '%y' $jpgfile)" 

doesn't work because newermt seems to perform a strict inequality comparison (same with newer). In addition newer... options are not really bullet-proof since searching the CR2 of an old JPG could find the CR2 of a newer homonym.


  • Am i overlooking an option in find (GNU findutils 4.7.0-git)?
  • Or is there an elegant way to do this (taking in account that files with the same name are normally years apart)?
  • "Long list"????
    – xenoid
    Aug 19 '18 at 10:56

With zsh, you can do

autoload age

ls -ld -- **/$file:t:e.CR2 (De'(age :$file :$file)')

To find the something.CR2 files that are exactly the same age as $file.

  • Thanks for the tip, even if I shall remain with bash for the time being. I found that my starting hypothesis was wrong anyway, timestamps look the same because they are rounded/truncated to the nearest even second, but there are a few cases where the time stamps are off by two seconds.
    – xenoid
    Aug 24 '18 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.