Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 6 down vote accepted

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

for file in ??-??-??.png ; do
    mv "$file" $i.png
    let i++
share|improve this answer
ARGHMETICS!! I know, they are frustrating.. – user13742 Feb 20 '12 at 16:36
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 posting your answer, you agree to the privacy policy and terms of service.

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