Article 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
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!
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!
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:
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:
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.
- Renaming directories can only be done with the
- Remember to enclose the ‘To’ and ‘From’ parameters in