rename file with zmv

5

I just discovered zmv this morning.

To test it i have created a few files.

test1.txt test2.txt test3.txt

I'm trying to rename them with zmv I'd like to get this output:

test-1.txt test-2.txt test-3.txt

when I try to use wildcards I get an error message:

zmv '(*)([0-9]{1,})(.txt)' '$1-$2$3'
zmv: error: number of wildcards in each pattern must match

Am i doing something wrong (answer has to be yes :) )

Yannick Schall

Posted 2012-03-14T11:56:17.547

Reputation: 173

@OliverSalzburg: It's not that it's greedy by default (backtracking takes care of what I think you're trying to fix). The problem is that * is applying to (, since he wrote * instead of .*. That's probably why zmv thinks there aren't enough capturing groups. – Peter Cordes – 2015-03-01T09:45:31.943

If the first parameter is a regular expression and it is greedy by default, try ([^0-9]*) as the first part maybe. – Der Hochstapler – 2012-03-14T12:04:38.473

What zsh version? Do you have zmv aliased? – Gilles 'SO- stop being evil' – 2012-03-14T22:38:57.213

zsh 4.3.9, autoload -U zmv, alias zmv="noglob zmv -W" – Yannick Schall – 2012-03-14T23:26:24.470

Answers

2

This is an extra-ordinarily late reply since I also ran into this problem and was breaking my head over it. I finally figured out that the zsh customization layer I used (yadr) was aliasing zmv. alias zmv="noglob zmv -W". I believe even the popular oh-my-zsh layer does htis.

Check whether your zmv is aliased. If so unalias it using unalias zmv and that horrible number of wildcards must match error should disappear now

lenkite

Posted 2012-03-14T11:56:17.547

Reputation: 121

2

Do you only want to add a - to the filename? Try it like this:

zmv -n 'test(*).txt' 'test-${1}.txt'

The -n switch shows you what would happen without really performing any action.

Sebastian Stumpf

Posted 2012-03-14T11:56:17.547

Reputation: 604

Tried just that, but I get the same error message. I'm running zsh 4.3.9 on osx. not sure if it changes anything. – Yannick Schall – 2012-03-14T12:56:29.657

0

If the first arg is supposed to be a regex, the problem is probably that you used * instead of .*. (* is the 0-or-more operator, . matches any single character).

So the first paren probably got taken literally as the arg of the * operator, instead of opening a capture group.

Try out http://www.regexr.com/ for an interactive regex tutorial / tester / builder thing.

Peter Cordes

Posted 2012-03-14T11:56:17.547

Reputation: 3 141