I have a large set of audio files that all begin with a variable length number, followed by a space-dash-space (i.e. " - "
). The text after this is variable. Some of the files include:
12 - Buckle my Shoe.mp3
42 - The Meaning of Life.m4a
12321 - Palindromes and other stuff.wav
How can I convert, for example, all of the file names so that the leading integer is padded up to 6 characters in width, with leading zeros? i.e.:
000012 - Buckle my Shoe.mp3
000042 - The Meaning of Life.m4a
012321 - Palindromes and other stuff.wav
I'm doing this because my audiobook player doesn't sort files properly unless the numbers in the file name are leading-zero-padded and fixed width.
I have something that's a lengthy bash script that almost accomplishes this, but I was hoping there's a simpler one-liner I could use via the command-line. Also, I was hoping to learn the minimal regex for this.
Also, if there's a quick-and-easy way to calculate the longest leading integer and use that instead of my arbitrary 6
, that's a plus too!
Thank you.
If you have the Perl version of rename
, you can do something like this:
rename 's/^(\d+)/sprintf "%06d", $1/e' *