‹ jan0sch.de

Mass renaming files with zmv


The zsh contains a very usefull extension called zmv. You can load it via autoload -U zmv. The man page is located under zshcontrib. A very usefull flag is -n which does not execute the command but prints the command that would be executed. Use this if you are unsure about your patterns.

Here are two simple examples:

Replace all spaces in filenames with underscores.

% zmv '* *' '$f:gs/ /_'

Rename all files according to a pattern.

% zmv '*.(*).(*).([0-9][0-9])*.mkv' '$1_$2-$3.mkv'

This one renames all files matching Garbage.Important.Important.Two-Digits-Followed-by-Garbage.mkv into Important_Important-Two-Digits.mkv.