mmv: Mass moving and renaming files
June 13th, 2007 edited by anaArticle submitted by Ferry Boender. We are running out of articles ! Please help DPOTD and submit good articles about software you like NOW !
Mmv is command-line tool which allows the user to move, rename, copy, append and link large amounts of files with a single command. The tool is especially useful when you need to rename a lot of files that have similar filenames, yet subtle differences.
Although mmv does more than just renaming files, this article will focus only on renaming because that is what I use it for the most. The tool is best explained using an example.
Suppose you have the following files in a directory:
foo1.png foo2.png bar3.png
You want all the files that start with ‘foo’ to instead start with ‘bar’. In this case it could easily be done manually, but suppose there are hundreds of files! You’d soon be forced to start shell-scripting some solution. But mmv is the perfect tool for this job:
mmv "foo*.png" "bar#1.png"
The above command will result in the following files:
bar1.png bar2.png bar3.png
Explanation
From pattern
Mmv matches the files using the wildcard you gave (the ‘From’ pattern). Then it will rename the matched files according to the second argument (the ‘To’ pattern). The ‘From’ pattern can take all the usual shell wildcards such as ‘*’, ‘?’ and ‘[]‘. Remember that you need to enclose the patterns with quotes, otherwise they will be expanded by the shell and mmv won’t understand them!
To pattern
The ‘#1′ in the ‘To’ pattern is a wildcard index. It matches the first wildcard found in the ‘From’ pattern. A ‘#2′ in the ‘To’ pattern would match the second second wildcard, etc. Mmv replaces any occurrences of wildcard indexes with the text for the corresponding wildcard in the ‘From’ pattern. In the example above, ‘#1′ matches the number after ‘foo’ and in front of the period. Note that ‘??’ are actually two wildcards, both of which match a single character!
More examples
The ‘From’ and ‘To’ pattern can also be used to switch around stuff in filenames:
abc_123.txt def_456.txt ghi_789.txt mmv "*_*.txt" "#2_#1.txt"
Would result in:
123_abc.txt 456_def.txt 789_ghi.txt
Another nifty trick mmv can do is changing the case of text matched by a wildcard. To do this, you place a ‘l’ (lowercase) or ‘u’ (uppercase) between the ‘#’ and the number in the ‘To’ pattern:
john.txt pete.txt mmv "?*.txt" "#u1#2.txt"
This results in:
John.txt Pete.txt
Safety
Mmv tries to be as safe as possible to avoid collisions in renaming which might cause files to be deleted. If, for instance, the result of the renaming would cause two different files to get the same name (thereby overwriting one), mmv will issue a collision warning and abort.
Mmv also tries to gracefully handle a rename that causes one of the resulting filenames to be identical to one of the source filenames. For instance:
a aa mmv "*" "a#1"
This does not overwrite the ‘aa’ file with the ‘a’ file but instead results, as expected, in:
aa aaa
Availability
Mmv has been available in Debian at least since v3.1 (’Sarge’) and in Ubuntu since Warty. apt-get install mmv
will install it for you.
Notes
- Renaming directories can only be done with the
-r
switch. - Remember to enclose the ‘To’ and ‘From’ parameters in
quotes !
June 13th, 2007 at 7:18 am
for all to know…
There are also ‘rename’ perl script, that uses perl regex for renaming.
Dunno whick package it is from, though (but I didn’t install it myself!).
There are also few GUI tools of same purpose like krename.
And convmv for encoding conversion.
June 13th, 2007 at 8:39 am
Long live Thunar’s graphical “Bulk Renamer”. That’s what I call a file browser ;)
June 13th, 2007 at 9:37 pm
I use
cmv for some years now and it rocks.
cmv ’s/([0-9]+)/_\1_/g’ *
cmv ‘tr/A-Z/a-z/’ *
June 14th, 2007 at 3:07 am
I prefer the “rename” command… which allows you to use Perl/Python regular expressions.
I already know shell wildcards, Perl/Python REs, and Emacs REs. I don’t much want to learn another set!!
June 15th, 2007 at 2:06 am
FWIW, rename is actually /usr/bin/prename which is included in the standard perl package (that is, if you have perl, you have rename).
June 18th, 2007 at 9:21 pm
Another similar utility, less versatile but slightly simpler, is wren:
http://www.ibiblio.org/pub/Linux/utils/file/wren-1.00.tgz
June 18th, 2007 at 10:50 pm
Years (many, many years) ago I wrote the following, which I called “mvall”:
#! /bin/bash
# mvall is set up to mimic the wildcard aspects of the VMS “RENAME” command
# The syntax is as follows:
#
# mvall string1 string2 filename
#
# changes the first occurance of ’string1′ in ‘filename’ to ’string2′
# if this results in a new name, then ‘filename’ is renamed. Wildcards
# in filename are acceptable, but wildcards in string1 or string2
# will mess up the count. Therefore, be sure to escape wildcard
# characters in string1 and string2 if they are needed.
#
p1=$1
p2=$2
# echo $p1 $p2
shift 2
for file in $*
do
newpar=`echo $file | sed “s/$p1/$p2/”`
mv $file $newpar
done
June 22nd, 2007 at 4:38 am
Renameutils worth a mention:
http://www.nongnu.org/renameutils/
April 15th, 2008 at 11:16 pm
Thanks!
What is wrong with open-source authors, that they never provide examples in their man pages? I could have spent 20 minutes struggling through the mmv man page to figure out what the example
mmv “foo*.png” “bar#1.png”
tells me in a few seconds. This is endemic to man pages. Not 1 in 10 provides a single example of how to use the command.
May 30th, 2009 at 8:59 am
I agree with Phil, an example should be mandatory for all man pages.