www-gem words

Mass renaming in zsh

Published on

zsh Renaming multiple files in one commandline may be challenging… until you discover that zsh comes with a built-in feature called zmv which is unfortunately disabled by default. So the first thing you want to do is to activate it by simply adding this line to your .zshrc:

autoload zmv

Then reload zsh and you’re done.

When you add a feature you usually expect to have to deal with a bunch of configuration as well. Good news here: there’s no additional config necessary !

The basic structure of a zmv call is zmv 'input_pattern' 'output_pattern'. The patterns are included in single quotes and you would have to use brackets to respect the standard Z Shell syntax to match the enclosed patterns but you can make your life easier by removing the use of quotes by adding the tag -W to the zmv command, and removing the use of brackets by prepanding your command with noglob. Hence, let’s take the example of the files:

photo1-onJan01.jpg
photo2-onJan01.jpg
photo3-onJan01.jpg

Using:

noglob zmv -W photo*Jan*.jpg myphoto*Dec*.jpg

Will rename all the files like that:

myphoto1-onDec01.jpg
myphoto2-onDec01.jpg
myphoto3-onDec01.jpg

Imagine combining your new super power with other tools like fd and you can see how efficient you can be at mass renaming files in a single commandline ! Of course, to save you time, create an alias (rn for example) for your new noglob zmv -W command.


Thanks for your read. Hope it's been useful to you.


Interact with this post using Mastodon or

Comment on wwwgem's post

Copy and paste this URL into the search field of your favourite Fediverse app or the web interface of your Mastodon server.

✄ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈ ┈

More food for thoughts? Check other posts about: #Zsh