MMV Command

mmv is a very powerful tool to rename files and directories based on patterns. The man page is very informative and well written. Check it. But nevertheless, here are the basic commands.

rename files in the current directory

Lets assume you have a file called foo.mp3 and want to rename it to bar.mp3. Here’s the command.

$ mmv 'foo.mp3' 'bar.mp3'

Well, thats simple and for that you wouldn’t need mmv. You could have used mv instead. The good thing about mmv is, when you have to rename multiple files. Lets assume you have a directory containing 100 files called foo0.mp3, foo2.mp3, ..., foo99.mp3. To change the filenames to bar0.mp3, bar1.mp3, ..., bar99.mp3, you would use

$ mmv 'foo*.mp3' 'bar#1.mp3'

You can access every wildcard used in the from pattern by the number of its occurence. The substituted string by this wildcard will be used in the to pattern. For example, to change the file foo.bar to bar.foo, you could use

$ mmv '*.*' '#2.#1'

rename files in different directories

There is a special wildcard if you want to rename files in multiple directories. Assume you have files called foo1, foo2, ..., foo99 distributed in multiple subdirectories. You know that all files are text files and want therfore change the filenames to foo1.txt, foo2.txt, ..., foo99.txt. Change to the directory containing the subdirectories with the files in question. Then you can use

$ mmv ';foo*' '#1foo#2.txt'

Note that there is no trailing slash after #1 because ; is always empty or ends with a slash (’/’). If the files are spread across the whole filesystem, you could use instead of the above

$ mmv '/;foo*' '#1foo#2.txt'

rename directories

If you want to rename directories, you have to use the -r option.

$ mmv -r '*dir*' '#1new_dir#2'

When renaming directories recursively, you must not specify the path (see man mmv)

$mmv -r ';*dir*' '#2new_dir#3'
 
wiki/mmv.txt · Last modified: 07.01.2008 19:39 by eglimi
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki