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)
And the next one should be:
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".
CodePudding user response:
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.
CodePudding user response:
If you want to maintain the numeric sort order from the original files:
#!/bin/bash
stamp=1666175710 # "Seconds since epoc, manually entered"
readarray -d '' images < <(printf '%s\0' *.png | sort -zV)
for i in "${images[@]}"
do
mv "$i" "$(date -d "@${stamp}" " %Y%m%d%H%M%S").png"
stamp=$(( stamp 1 ))
done
I also chose a naming closely related to iso-8601 which will allow for the natural sorting of the renamed files.