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:


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##*.}"
  • You could apply one of the answers from apple.stackexchange.com/questions/29320/… to get more precision.
    – nohillside
    Jul 10, 2021 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, 2021 at 14:00


You must log in to answer this question.

Browse other questions tagged .