Possible Duplicate:
Batch renaming files

I want to rename files using their existing name as a base for the new one.

So if I can ls these files with

ls blue*+(.png)

I'd want to rename them something like

mv blue$(*)+(.png) $(1).png

except that doesn't work obviously. Is there syntax for these kind of variables in bash globbing or is there an easier way?

marked as duplicate by Gilles, jasonwryan, Renan, daisy, jw013 Nov 27 '12 at 16:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


The portable way to do this is

for f in blue*.png; do mv -- "$f" "${f#blue}"; done

All this requires is mv and works in not just bash but also any POSIX compatible shell which supports standard POSIX parameter expansion. No need for your system to have zsh or a particular version of rename installed.

  • can you talk me through the mv part of this command? thanks – user1561108 Nov 26 '12 at 21:17
  • @user1561108 The ${f#blue} part removes the blue prefix from the beginning of $f. See the link I added in the edit for more details. – jw013 Nov 26 '12 at 21:33

I don't see any easy way to do this in bash, but of course you can break the problem into several steps like cutting the prefix, remembering the extension and then replacing the several instances of the extension with single one.

for file in blue*+(.png); do tmp="${file#blue}"; extension="${file##*.}"; echo "$file" "${tmp/%+(.$extension)/}.$extension";done

if you are happy with the results, just type...


and all the magic will happen. :)


With bash:

zsh -c '
  autoload -U zmv
  zmv "blue(*).png" "\$1.png"'

;-). Or more seriously, you'll find that zsh's zmv is a very powerful tool for all sorts of renaming jobs. Contrary to scripts you may come up with, that tool (or any other tool dedicated for the task like mmv or rename) takes extra care not to clobber files and is more likely to be more robust in corner cases.

  • 1
    title says Bash? – user1561108 Nov 26 '12 at 21:06

On Debian and derivatives (including Ubuntu), there's a rename command which can do what you want:

rename -n "s/\+\.png/.png/" blue*+.png

Remove the -n flag after testing to actualy apply the rename.

This rename command is a Perl script, not to be confused with the rename command from the util-linux suite. Other distributions may provide it as prename or rename.pl or not at all.

Not the answer you're looking for? Browse other questions tagged or ask your own question.