Mass renaming numbered files in bash

Today I stumbled upon a very peculiar issue –  When adding files to a playlist in smplayer, it sorts them in a rather annoying sequence, instead of what one would expect, e.g.

part00.mp4
part01.mp4
part02.mp4
part03.mp4

part09.mp4
part100.mp4
part101.mp4

part109.mp4
part10.mp4
part110.mp4
part111.mp4

and so on. Obviously, those files ain’t playing in the sequence I would want them to. In order for the files to be ordered nicely, all filenames containing double-digits need to be renamed to triple digit, e.g. part10.mp4 becomes part010.mp4. 100 times over. Now I could sit renaming files all day, or summon Bash to the rescue:

for i in `ls part[0-9][0-9].*`; do \
    mv $i `echo ${i}|sed -e ‘s/part\([0-9][0-9]\)\.mp4/part0\1\.mp4/’`; \
done

Problem solved. I can now happily watch the files in the sequence I want them to be.

Advertisements

Author: Nikolay

Computer enthusiast, from as far back as I can remember.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s