Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 6 down vote accepted

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
share|improve this answer
    
ARGHMETICS!! I know, they are frustrating.. –  Herman Torjussen 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
add comment

Your Answer

 
discard

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.