Find all filename.ext1 where no filename.ext2 exists in OS X or the shell

I need to create both TIF and JPG versions of a large set of images.

All JPG images already exist, but only a part of the TIF images. Is there an easy way to search a directory to find all JPG files that have no corresponding TIF file (i.e. a file with the same name but with different file extension)?


Perl one-liner:

find . -name '*.jpg'|perl -nle 's/\.jpg//;unless(-f "$_.tif"){print "$_.jpg"}' 

In 63 characters:

find . -name '*.jpg'|perl -pnle 's/.jpg//;-f "$_.tif"||"$_.jpg"' 

Assuming all images are in the images directory and has the .jpg suffix, the following little script will print out all image files that has no corresponding .tif file on UNIX:

#!/bin/sh

find images/ -type f -name "*.jpg" |
while read j; do
  t=${j%.jpg}.tif
  if [ ! -f "$t" ]; then
    echo "Lacking tif file: " $j
  fi
done

Paste this to a file, and save it a folder above the one where your images are stored. You could call it find-images. For example:

├── find-images
└── images/
    ├── 1.jpg
    └── 2.jpg
    └── ...

Now, open Utilities/Terminal.app, and use the cd command to navigate to the folder where your script is, e.g. if the script is on your Desktop, just enter cd Desktop.

Then, enter chmod +x find-images. Now you can run the script by just calling ./find-images.


I'll use Python, since it is cross-platform. First put your jpg and tif files in separate folders.

import os

jpgPath = "path/to/jpgs/folder"
tifPath = "path/to/tifs/folder"

jpgList = [item.rsplit(".", 1)[0] for item in os.listdir(jpgPath)]
tifList = [item.rsplit(".", 1)[0] for item in os.listdir(tifPath)]

diffList = [item for item in jpgList if item not in tifList]

print diffList

Then, save this script to a file, somewhere on your hard drive, maybe under the name find-images.

Now, open Utilities/Terminal.app, and enter python /path/to/file. For example, if you saved it on your Desktop, it would be python ~/Desktop/find-images (since ~ is a shortcut to your home folder). Here, you can run a more detailed explanation on running Python files on your Mac.


Ah, I felt like adding this. Here's the solution in Ruby, which prints every image that has no TIF counterpart:

jpg = Dir["*.jpg"]
tif = Dir["*.tif"]    
[jpg, tif].each { |a| a.map!{ |f| f[0..-5]} }
puts jpg - tif

Put it into a file which resides in the same folder as your images, save it as find-images, and run it in Terminal by entering ruby find-images.

If you don't know Ruby, don't ask how it works, might take a while to explain :P