Moving or Renaming Multiple Files – The Easy (zsh) Way

Linux workers like you and me often need to move a bunch of files. For example, you want to rename all *.dat files into *.dat_save, or you want to rename all files foo.* into something like bar.*. This, however, is not easy to do using the move command as 1) “mv” only supports a single destination file or directory and 2) the shell tries to expand patterns like “*.dat” into e.g. “a.dat b.dat c.dat” before executing the command. The typical workaround is to write a for loop like “for f in *.dat; do mv $f ${f/dat/dat_save}; done“. But it goes much easier if you use the power of zsh, which is the superior shell anyway. ;)

Insert the following two lines into your .zshrc

autoload -U zmv
alias mmv='noglob zmv -W'

The first line activates the zmv command, an extended move command provided by the zsh. The second line creates an alias for a simplified invocation of that command. All of a sudden, you can write something like “mmv *.dat *.dat_old” or “mmv foo.* bar.*” into a newly opened terminal and it will do as you expect! You can even invoke “mmv **/*2008.mp3 **/*2009.mp3” and all matching files residing in any subdirectory are renamed according to the pattern as well.

2 Responses to “Moving or Renaming Multiple Files – The Easy (zsh) Way”

  1. *bows*

  2. my 3 hours search ended happily with your post… “bows” indeed