Issue
So, I generated like thousands of images that I need to rename with this format:
19-10-2022 17-35-10 (HH:MM:SS format)
19-10-2022 17-35-11 (increases by one second) and so on.
Also, my images are perfectly numbered (like 1.png, 2.png, 3.png and so on)
Is there any way to archieve this with a bash script or something? I would like to specify the starting date as well.
Edit: I have already searched for this question all over internet and I couldn't find any useful information. All I find is how to append regular dates and not a custom one that increases by one second like I am asking here.
Edit 2: I need it to be a different starting date than "now".
Solution
Convert starting date to number of seconds since epoch, then add number from file name (or do any other arithmetics):
#!/bin/bash
START=$(date +"%s" -d "2022-10-20 13:00")
for F in *.png; do
N="${F%.png}"
DATE=$(date +"%d-%m-%Y %H-%M-%S" -d@$((${START}+${N})))
mv -v "${F}" "${DATE}.png"
done
UPD: this will work with perfectly named files but will fail on non-numerical names. Also it rename several files to the same name if you have something like 7.png
, 07.png
, 007.png
. If you want to handle any file names, just discard original name and increase START
by 1 for each rename.
Answered By - dimich Answer Checked By - Candace Johnson (WPSolving Volunteer)