1

After taking a look at this answer and adapting to my needs, I am stuck when there are files with identical timestamps.

I need to rename all files in a folder, including the date and timestamp at end. However, if any two files share the same date and timestamp, then my shell script doesn't work property. In that case, I thought the best solution would be to add a number, incrementing by one, each time there is a file with an identical timestamp.

The files should look like this:

photo_2019_12_23_22_20_35_001.jpg
photo_2019_12_23_22_20_35_002.jpg
photo_2019_12_23_22_20_35_003.jpg
photo_2019_12_23_22_20_36_001.jpg

Here is the code as I have it now:

for f in *.*; do 
    mv -n "$f" "photo-$(stat -f'%SB' -t "%Y_%m_%d_%H_%M_%S" "$f").${f##*.}"
done
nohillside
  • 100,768
matt
  • 137
  • 2
  • 8
  • You could apply one of the answers from https://apple.stackexchange.com/questions/29320/printing-the-current-date-time-with-sub-second-precision to get more precision. – nohillside Jul 10 '21 at 13:57
  • Instead of a sequential number, you could append a checksum, such as output by sum. Or the first few characters of md5. – lhf Jul 10 '21 at 14:00

0 Answers0