Skip to content

Instantly share code, notes, and snippets.

@rjames86
Last active February 24, 2024 05:03
Show Gist options
  • Save rjames86/33b9af12548adf091a26 to your computer and use it in GitHub Desktop.
Save rjames86/33b9af12548adf091a26 to your computer and use it in GitHub Desktop.

Last updated: 2017-03-18

Searching for Files

Find images in a directory that don't have a DateTimeOriginal

exiftool -filename -filemodifydate -createdate -r -if '(not $datetimeoriginal) and $filetype eq "JPEG"' .

###Output photos that don't have datetimeoriginal to a CSV### Note this can take a long time if you have a lot of jpgs

# You'll need to set your Dropbox folder path. I have mine set as a global variable
OLDFILE="$DROPBOX_PERSONAL/nodates.csv"

FILECOUNT=$(mdfind -count -onlyin "$DROPBOX_PERSONAL" 'kMDItemKind =="JPEG image"')
while IFS= read -r -d '' n; do
    FILECOUNT=$(( $FILECOUNT - 1 ))
    if grep -q "$n" "$OLDFILE"; then
        echo "Skipping $n"
        continue
    fi

    echo -ne "Remaining: $FILECOUNT\r"
    exiftool -q -if '(not $datetimeoriginal or ($datetimeoriginal eq "0000:00:00 00:00:00"))' -csv -common "$n" | sed 1d >> "$DROPBOX_PERSONAL/nodates.csv"
done < <( mdfind -onlyin "$DROPBOX_PERSONAL" 'kMDItemKind =="JPEG image"' -0 )

See files File Modify Date recursively in a directory who don't have datetimeoriginal set

exiftool -filemodifydate -r -if '(not $datetimeoriginal or ($datetimeoriginal eq "0000:00:00 00:00:00")) and ($filetype eq "JPEG")' .

Modifying Files

Create Captions From a Filename

The command alone will put the full filename in the comments. If you want to add the filename without the extension, add the example exiftool config file found here

# This is the command if you have the config installed
exiftool '-Comment<BaseName' '-UserComment<BaseName' .

# Getting the filename without the config file, but with a for-loop
for i in *; do 
	FileName="${i%%.*}"
	exiftool "-Comment=$FileName" "-UserComment=$FileName" "$i";
done

###Change JPG to jpg and MOV to mov in filenames###

for i in *.JPG; do mv "$i" "${i%%.JPG}.jpg"; done; !#:gs/JPG/MOV/:gs/jpg/mov/

Recursively

find /path/to/directory -name *JPG -exec sh -c 'mv "$0" "${0%%.JPG}.jpg"; echo "Moved $0 to ${0%%.JPG}.jpg"' {} \;

###Change last created and modified for files in a directory###

The date syntax has to be YYYY:MM:DD HH:MM:SS

Option 1:

find . -name "*.jpg" | while read filename;  
    exiftool "-AllDates=1986:11:05 12:00:00" "$filename";
done

Option 2:

exiftool "-AllDates=1986:11:05 12:00:00" -if '$filetype eq "JPEG"' .

Timeshift Photos by One Year

Info at http://www.sno.phy.queensu.ca/~phil/exiftool/Shift.html

exiftool "-AllDates+=1:0:0 0" .

Rename files to datestamp

Filename looks like 2014-01-01 12:00:00.jpg and will append -NUM if DateTimeOriginal is the same for multiple files

exiftool '-FileName<DateTimeOriginal' -d "%Y-%m-%d %H.%M.%S%%-c.%%e" .  

Rename Files to With Milliseconds

Good for burst photos where the seconds are all the same. If milliseconds are only out to 2 digits, use ${SubSecCreateDate} instead

Found at http://u88.n24.queensu.ca/exiftool/forum/index.php?topic=2736.0

exiftool -v '-Filename<${datetimeoriginal}${subsectimeoriginal;$_.=0 x(3-length)}.%e' -d %Y%m%d_%H%M%S .

Update any photo that doesn't have DateTimeOriginal to have it based on file modify date

exiftool '-datetimeoriginal<filemodifydate' -if '(not $datetimeoriginal or ($datetimeoriginal eq "0000:00:00 00:00:00")) and ($filetype eq "JPEG")' .

Set DateTimeOriginal to Any Arbitrary Timestamp

exiftool '-datetimeoriginal=2015:01:18 12:00:00' .

Moving/Copying Files

###Copy directory recursively into organized folder###

exiftool -o ~/dummy/ -if '$filesize# > 300000' '-Directory<CreateDate' -d ~/Desktop/old_photos2/%Y/%m\ %B -r ~/Desktop/iPhoto\ Library/

-o ~/dummy This flag is to copy, not move. The directory is a fallback if the flag isn't available on the given photo. Good if using something like DateTimeOriginal

-if '$filesize# > 300000' gets files that are over 300kB. I was parsing an iPhoto library where there were thumbnails. The # turns the value to a number. you can use the flag -n to turn all values to numbers

'-Directory<CreateDate' Create directories based on the CreateDate of the photos

-d ~/Desktop/old_photos/%Y/%m\ %B Create folders with the given date format.

-r Run recursively

Extras

###Create KML from geotagged photos###

DESKTOP=$HOME/Desktop
cat $DESKTOP/kml-start.fmt > out.kml
exiftool -n -r -q -p $DESKTOP/kml-placemark.fmt . >> out.kml
cat $DESKTOP/kml-end.fmt >> out.kml

Create CSV of Geo Information

exiftool -csv -filename -imagesize -gps:GPSLatitude -gps:GPSLongitude ./ > long.csv

Update Notes

  • 2017-03-18 - added command to create comments from a filename
  • 2015-08-11 - added line to rename files based on milliseconds
  • 2015-01-18 - added line on how to set a date for a particular photo(s)
  • 2014-12-26 - Adding new recursive command to rename JPG to jpg. Info on using sh with -exec here
  • 2014-12-25 - Added line for copying photos into organized photos.
@mceachen
Copy link

Thanks for this! Github's Markdown parser requires a space after hashes to render headings. I fixed several in my fork, if you want to merge that.

@Krzysiu
Copy link

Krzysiu commented Jul 17, 2017

exiftool -P -if "not ($GPSLongitude and $GPSLatitude)" -geotag "f:\gpx\*.gpx" -R "f:\foto"

Set GPS data basing on GPX from all files from f:\gpx\ directory to all photos from f:\foto (recursive), but only for there which don't have lon and lat already. If there are files which doesn't match GPS/GPX data, they will be just ignored. I recommend using it with GPSLogger (opensource app for Android). This way you can quickly geotag all images using all input files.

PS: Exiftool forum is one of the best. Author is so helpful. He solved a lot of my problems. I'm very serious here.

@keyurgolani
Copy link

There a way to change all dates of all files with "*.jpg" but keep the timestamp same? Think that might come in handy.

@rjames86
Copy link
Author

@keyurgolani

exiftool "-AllDates=1986:11:05 12:00:00" . will change all of the dates to the same thing for a directory

@dnozay
Copy link

dnozay commented Jul 24, 2019

@rjames86 can you please add what licensing terms to use? CC0 / BSD / MIT / Perl / etc.

@dnozay
Copy link

dnozay commented Jul 26, 2019

Added more changes in https://gist.github.com/dnozay/955037f077b6dec87aa9b1eb552caf8d in case you want to merge them. Thank you!

@lesar
Copy link

lesar commented Apr 4, 2020

You save me a lot of time: all to try to do complex operation and no were to look at simple like

exiftool "-DateTimeOriginal=2020:04:04 09:04:02" ./IMG_20200404_090402.jpeg

thanks ;-)

@marissadulaney
Copy link

Anyone try time shifting recently? I just (re)downloaded exiftool to my mac and used exiftool "-AllDates+=1:0:0 0" DIR to fix my photos (shift from 2019 to 2020 since I didn't set my camera earlier this year). But in Finder the result is that all the dates are changed to today at the time I ran the command. I did this exact thing in 2014 (per terminal history), and I'm pretty sure it worked then...

@rvalle
Copy link

rvalle commented Aug 2, 2020

I want to move old photos. Like photos older than 30 days should be renamed:

Something like this:

rename if create_date <= now() - 30 days

if it possible to do such a thing with exiftool expressions?

@automazione
Copy link

Anyone try time shifting recently? I just (re)downloaded exiftool to my mac and used exiftool "-AllDates+=1:0:0 0" DIR to fix my photos (shift from 2019 to 2020 since I didn't set my camera earlier this year). But in Finder the result is that all the dates are changed to today at the time I ran the command. I did this exact thing in 2014 (per terminal history), and I'm pretty sure it worked then...

Same problem here, did you find any workaround?

@marissadulaney
Copy link

Anyone try time shifting recently? I just (re)downloaded exiftool to my mac and used exiftool "-AllDates+=1:0:0 0" DIR to fix my photos (shift from 2019 to 2020 since I didn't set my camera earlier this year). But in Finder the result is that all the dates are changed to today at the time I ran the command. I did this exact thing in 2014 (per terminal history), and I'm pretty sure it worked then...

Same problem here, did you find any workaround?

I didn’t. Stopped trying and banging my head on it. If you find something let me know.

@automazione
Copy link

https://exiftool.org/forum/index.php?topic=10922.msg58284#msg58284 explained my misunderstanding:
_

"The AllDates only affects three tags, DateTimeOriginal, CreateDate, and ModifyDate. Odds are the tag that changed is not EXIF but the file system dates, FileModifyDate or FileCreateDate. "

_

Since I just needed to move the dates up one year (wrong clock setup on camera) this did the wanted result:

exiftool '-AllDates+=1:0:0 0' '-FileModifyDate+=1:0:0 0' '-FileCreateDate+=1:0:0 0' DIR

Anyone try time shifting recently? I just (re)downloaded exiftool to my mac and used exiftool "-AllDates+=1:0:0 0" DIR to fix my photos (shift from 2019 to 2020 since I didn't set my camera earlier this year). But in Finder the result is that all the dates are changed to today at the time I ran the command. I did this exact thing in 2014 (per terminal history), and I'm pretty sure it worked then...

Same problem here, did you find any workaround?

I didn’t. Stopped trying and banging my head on it. If you find something let me know.

@SergioShz
Copy link

SergioShz commented Dec 15, 2021

awesome, thanks!! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment