4

There is a bunch of files of the following format hh-mm-ss.png where hh, mm, ss are hour, minute and second. How can I rename all these files in the directory in a sequential way from 1.png til n.png so that all files sorted by hour, then by minutes, then by seconds. For example,

12-56-11.png -> 3.png
11-13-49.png -> 1.png
12-55-55.png -> 2.png
6

Here is a solution using a for loop and bash arithmetic:

i=1
for file in ??-??-??.png ; do
    mv "$file" $i.png
    let i++
done
  • ARGHMETICS!! I know, they are frustrating.. – user13742 Feb 20 '12 at 16:36
  • 3
    You should note that this only works because bash expands "??-??-??.png" in the order Sergey asked for. That is, "hh-mm-ss" sorts in lexical order by bash expansion, but that just so happens to correspond to ascending timestamp order. – Bruce Ediger Feb 20 '12 at 16:51
  • Just as a cross-reference: In Bash, are wildcard expansions guaranteed to be in order?, – Peter.O Feb 20 '12 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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