Super User is a question and answer site for computer enthusiasts and power users. 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

I have the following files in a single directory:

1-1 - different text for each file here.txt
1-2 - different text for each file here.txt
.
.
1-9 - different text for each file here.txt
1-10 - different text for each file here.txt
1-11 - different text for each file here.txt
1-12 - different text for each file here.txt
.
.


I want to rename the files all at once (using zmv running in zsh) to:

1-01 - different text for each file here.txt
1-02 - different text for each file here.txt
.
.
1-09 - different text for each file here.txt
1-10 - different text for each file here.txt
1-11 - different text for each file here.txt
1-12 - different text for each file here.txt


Here's what I tried: zmv '1-([0-9])(*)' '1-0$1$2'


This results in following file names:

1-01 - different text for each file here.txt
1-02 - different text for each file here.txt
.
.
1-09 - different text for each file here.txt
1-010 - different text for each file here.txt
1-011 - different text for each file here.txt
1-012 - different text for each file here.txt
.
.

So I thought I'd change the find pattern to zmv '1-([0-9]{1})(*)' '1-0$1$2' to match only files with a single digit after the hyphen, but this doesn't work (I just get zmv:232: no matches found: 1-([0-9]{1})(*)).

What am I doing wrong? Is this even possible using zmv or should I use something like sed in a for loop or something like that?

share|improve this question
up vote 4 down vote accepted

This should work for you:

zmv '1-([0-9])( *)' '1-0$1$2'

or

zmv '1-([0-9])([^0-9]*)' '1-0$1$2'

You just need to make the match include only one digit followed by a space or non-digit. With the way you had it, it was matching one digit followed by anything.

share|improve this answer
    
Argh, it was so simple, but I didn't see it! Thanks for opening my eyes. :) – Marco Peluso Feb 25 '11 at 23:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .