Skip to content

Instantly share code, notes, and snippets.

@nickbutcher
Created April 3, 2017 11:31
Show Gist options
  • Save nickbutcher/5774eef98fefe27c4c63e667b1b281e4 to your computer and use it in GitHub Desktop.
Save nickbutcher/5774eef98fefe27c4c63e667b1b281e4 to your computer and use it in GitHub Desktop.
A script for setting creation and modification dates on all files within a folder. Specify the first date in the script then all files are stamped in alphabetical order, increasing by 1s.
USAGE="Set the desired date/time in the script and then call: timestamp /path/to/folder"
DATE="022109002017" # month day hour min year
if (( $# != 1 )); then
echo "Illegal parameters"
echo $USAGE
exit 2
fi
if [ ! -d $1 ]; then
echo "$1 is not a directory"
echo $USAGE
exit 2
fi
SEC=0
# run through all files in the directory in ls order i.e. alphabetically
for i in $( ls "$1" ); do
TIMESTAMP=$(date -v"$SEC"S -j $DATE '+%m/%d/%y %H:%M:%S')
FILE="$1/$i"
# set both creation and modification timestamps
SetFile -d "${TIMESTAMP}" -m "${TIMESTAMP}" "${FILE}"
# increment the timestamp by 1 second
SEC=$((SEC + 1))
done
exit 0
@alexjlockwood
Copy link

alexjlockwood commented Oct 24, 2017

Hmm... I start getting errors after 60 images:

60S: Cannot apply date adjustment
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
ERROR: invalid date/time
61S: Cannot apply date adjustment
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
ERROR: invalid date/time
62S: Cannot apply date adjustment
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
ERROR: invalid date/time

@alexjlockwood
Copy link

This seems to work for me:

TOTAL_SECONDS=0
# run through all files in the directory in ls order i.e. alphabetically        
for i in $( ls "$1" ); do
  MIN=$((TOTAL_SECONDS / 60))
  SEC=$((TOTAL_SECONDS % 60))
  TIMESTAMP=$(date -v"$MIN"M -v"$SEC"S -j $DATE '+%m/%d/%y %H:%M:%S')
  FILE="$1/$i"
  # set both creation and modification timestamps                               
  SetFile -d "${TIMESTAMP}" -m "${TIMESTAMP}" "${FILE}"
  # increment the timestamp by 1 second                                         
  TOTAL_SECONDS=$((TOTAL_SECONDS + 1))
done

@saket
Copy link

saket commented May 8, 2019

I modified this a bit to increment hours as well,

# Run through all files in the directory in ls order i.e. alphabetically.        
for i in $( ls "$1" ); do
  HOUR=$((TOTAL_SECONDS / 3600))
  MIN=$(( ((TOTAL_SECONDS / 60)) % 60 ))
  SEC=$((TOTAL_SECONDS % 60))

  FILE="$1/$i"
  
  # Set both creation and modification timestamps.
  TIMESTAMP=$(date -v"$HOUR"H -v"$MIN"M -v"$SEC"S -j $DATE '+%m/%d/%y %H:%M:%S') 
  SetFile -d "${TIMESTAMP}" -m "${TIMESTAMP}" "${FILE}"

  # Leaving a gap of few minutes makes it possible to insert additional media 
  # to Google Photos later. This has to be in the magnitude of minutes because 
  # Google Photos doesn't allow changing the seconds value when updating a 
  # media's timestamps.
  TOTAL_SECONDS=$((TOTAL_SECONDS + 300))
done

@saket
Copy link

saket commented May 8, 2019

By the way, is anyone else still using this script? For some reason, Google Photos orders videos by their Last Opened timestamp, completely ignoring their created and modified timestamps. I had to spend many hours manually editing the timestamp of each video in my album.

I wish SpeakerDeck supported videos.

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