1

I have a bunch of image files spared into different folders with a false timestamp on the filename (1 hour delay) and i want to correct that according to the creation time.

I am now trying to write a **shell script** that can go through all the folders > extract the creation time and and replace it on the filename timestamp, below a small example:

Original filename with false timestamp:
Filename, Created Date, Modified Date
20180524010500530_FR785101.jpg, 2018-05-24 00:05:00, 2018-05-24 00:05:34

The output would be:
Filename, Created Date, Modified Date
20180524000500530_FR785101.jpg, 2018-05-24 00:05:00, 2018-05-24 00:05:34

Can Anyone tell me if that is possible with a shell script ? Then, can anyone give me some direction on how to use the ls command or any other command that can do the job ?

I have started to document myself on the use of the ls command but i still cannot figure out how i can extract the creation time and replace it into the filename timestamp, also i don't know how i can make the script run through all the folders and sub-folders containing the image files,

closed as off-topic by Kusalananda, Jeff Schaller, Stephen Rauch, Kiwy, G-Man Jun 29 '18 at 14:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Kusalananda, Jeff Schaller, Stephen Rauch, Kiwy, G-Man

  • to answer exactly to your question, yes it is possible. You should provide what you've done so we can help complete it or point you toward the right direction. Take the time to read the tour and welcom on Stack Exchange Unix & Linux. – Kiwy Jun 25 '18 at 11:56
  • I have updated my question, sorry for that :) – Yacine Walid Jun 25 '18 at 12:12
  • 3
    Does your system have creation time, most Unixes don't have that. They have last modified time. – ctrl-alt-delor Jun 25 '18 at 13:04
  • Yes, my system got creation and modification time – Yacine Walid Jun 25 '18 at 14:35
  • 1
    This has been cross posted (don't do that): stackoverflow.com/questions/51026011/… – Kusalananda Jun 28 '18 at 9:39
0

You can use stat command to get file creation time.

CREATION_TIME=`debugfs -R 'stat /path/to/file' /dev/sdaX | awk -F '-' '/crtime/{print $NF}'`

And you can change the modified date with touch command.

touch -d "$CREATION_TIME" file
0

Here is a script that will do that for you, it strictly uses the filename format you provided. Assuming all files start with the date time first and that only 1hr delay that needs to be fixed. It's using epoch time to subtract 1hr from the time in the filename and rename the files.

Change the directory in the script to your local one.

looper.sh

    #!/bin/bash

    echo "Sending directory to the past."

    for path in /path/to/directory/*.jpg; do
      filename=${path##*/}
      #Convert to date format
      oldDate="${filename:0:4}-${filename:4:2}-${filename:6:2} ${filename:8:2}:${filename:10:2}:${filename:12:2}"
      #Get the epoch date
      epochDate=$(date -d "$oldDate" +%s)
      #Subtract 1 hour
      epochDate=$(( $epochDate - 60*60 ))
      #Converting and formating the new date
      newDate=$(date -d @$epochDate +%Y%m%d%H%M%S)
      #New filename
      newFilename=$(dirname $path)/$newDate"${filename:14}"
      #Renaming the file
      mv $path $newFilename
    done

EDIT: Here is a script comparing the birth date before renaming. (Make sure to backup just in case)

#!/bin/bash

echo "Sending directory to the past."

for path in /path/to/directory/*.jpg; do
  filename=${path##*/}
  #Convert to date format
  oldDate="${filename:0:4}-${filename:4:2}-${filename:6:2} ${filename:8:2}:${filename:10:2}:${filename:12:2}"
  #Get the epoch date
  epochDate=$(date -d "$oldDate" +%s)
  #Birth date - Only works if your filesystem supports it.
  birthDate=$(stat $path | grep 'Birth:' | sed 's/\..*//' | awk '{print $2" "$3}')
  epochBirthDate=$(date -d "$birthDate" +%s)
  if [ $epochDate != $epochBirthDate ]; then
    #Subtract 1 hour
    epochDate=$(( $epochDate - 60*60 ))
    #Converting and formating the new date
    newDate=$(date -d @$epochDate +%Y%m%d%H%M%S)
    #New filename
    newFilename=$(dirname $path)/$newDate"${filename:14}"
    #Renaming the file
    mv $path $newFilename
  fi
done
  • Wow, that works like a charm, thank you so much, now i am trying to sort out the delayed filenames because i just realised that some of the filenames have the correct timestamp. Should i script something for comparing the creation date with the filename timestamp, or there is some command that can tell me exactly how much filenames have been corrected ? – Yacine Walid Jun 26 '18 at 14:23
  • Yes that would be a good addition if your filesystem supports it. Typically only Access, Modify, and Change timestamps are kept. You can get the birth time by typing stat filename, then use the datetime from that to compare to the filename datetime and change if required. – K. Kirilov Jun 26 '18 at 15:17
  • Hello @K.Kirilov now that is working for me and i wanted to ask if epochDate can also send the filename timestamp to the future instead of the past by writing maybe epochDate=$(( $epochDate + 60*60 )) ? Also, can i add some command to the script so that he generates a text file with how much filenames have the wrong timestamp after comparison with the Creation time like > wrong_timestamps.txt – Yacine Walid Jun 28 '18 at 7:07
  • Now that i am using this script, i am getting another error in the execution that says this <br/> date: invalid date '*.jp-g- :: ' <br/> stat: cannot stat '/volume1/Test_aar/*.jpg' : No such file or directory<br/> .filename_timestamp.sh: line 19: [: !=: unary operator expected] – Yacine Walid Jun 30 '18 at 10:59

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