LinuxCommandLibrary

mmv

Rename multiple files using wildcard patterns

TLDR

Rename all files with a certain extension to a different extension

$ mmv "*[.old_extension]" "#1[.new_extension]"
copy

Copy report6part4.txt to ./french/rapport6partie4.txt along with all similarly named files
$ mmv [[-c|--copy]] "[report*part*.txt]" "[./french/rapport#1partie#2.txt]"
copy

Append all .txt files into one file
$ mmv [[-a|--append]] "[*.txt]" "[all.txt]"
copy

Convert dates in filenames from "M-D-Y" format to "D-M-Y" format
$ mmv "[[0-1][0-9]-[0-3][0-9]-[0-9][0-9][0-9][0-9].txt]" "[#3#4-#1#2-#5#6#7#8.txt]"
copy

SYNOPSIS

mmv [options] oldpattern newpattern

PARAMETERS

-n
    No execute; display what would be done without actually doing it. This is a "dry run" mode.

-v
    Verbose mode; show each move/rename as it happens.

-a
    Automatic mode; don't ask questions, just do it (careful!).

-m
    Allow moving across file systems. Normally, mmv will refuse to move files to another file system.

-s
    Same directory. Requires both patterns to start with a directory name; moves files within that directory.

-d
    Make destination directory if it does not exist.

oldpattern
    The pattern to match against existing filenames. Supports wildcards.

newpattern
    The pattern to generate the new filenames. Can include backreferences to captured groups from oldpattern.

DESCRIPTION

The mmv command provides a powerful and flexible way to rename or move multiple files at once using wildcard patterns. Instead of repeatedly typing individual rename (mv) commands, mmv allows you to specify pattern transformations that are applied to a set of files. This command is particularly useful when you need to perform complex renamings based on parts of the existing filenames or when dealing with a large number of files.

It allows you to specify both an 'old' pattern to match existing filenames and a 'new' pattern that specifies how the files should be renamed or moved. The 'new' pattern can reference parts of the matched 'old' pattern using backreferences, making it easy to manipulate filenames. The command prompts for confirmation of the changes before they are executed, providing a safety net against unintended consequences. It offers options to skip confirmation, perform a 'dry run' to preview the changes without executing them, and handle various error conditions effectively.

CAVEATS

Use mmv with caution, especially with the -a option. Incorrect patterns can lead to significant data loss or corruption. Always test your patterns with -n first.

<B>BACKREFERENCES</B>

The newpattern can contain references to parts of the oldpattern that matched the wildcards. The sequences #1, #2, ..., #9 are replaced by the text matched by the corresponding wildcard in the oldpattern.

<B>EXAMPLES</B>

Rename all .html files to .htm:
mmv '*.html' '#1.htm'

Move all files in /tmp starting with 'foo' to /var/tmp and prepend 'moved_' to their names:
mmv -s '/tmp/foo*' '/var/tmp/moved_#1'

HISTORY

The mmv command has been available on Unix-like systems for a long time and is typically included in distributions to ease batch renaming tasks. It's a userland tool and the maintainership has changed over time.

SEE ALSO

mv(1), rename(1)

Copied to clipboard