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] source_pattern target_pattern

PARAMETERS

-m
    Performs a move operation. This is the default action if no other operation flag is specified.

-c
    Performs a copy operation, leaving the original files intact.

-l
    Creates hard links to the target files instead of moving or copying.

-s
    Creates symbolic (soft) links to the target files instead of moving or copying.

-o
    Overwrites existing destination files without prompting for confirmation.

-t
    Skips files that would overwrite an existing destination file without prompting.

-u
    Only moves or copies a file if the destination file is older than the source, or if the destination does not exist.

-n
    Performs a 'dry-run'. It shows what actions mmv would take without actually executing them, useful for verifying complex patterns.

-v
    Activates verbose mode, displaying each file operation as it is performed.

-a
    Includes hidden files (those starting with a dot '.') in the matching process. By default, hidden files are ignored unless explicitly matched by the pattern.

-x
    Ensures that wildcards in the source pattern do not match '.' or '..' directory entries, preventing unintended operations.

--
    Indicates the end of options. This allows the source_pattern or target_pattern to begin with a hyphen.

DESCRIPTION

mmv (mass move) is a powerful command-line utility for moving, copying, linking, or renaming multiple files based on pattern matching. Unlike the standard mv command, mmv excels at performing complex bulk operations by allowing users to specify a source pattern and a target pattern. It interprets wildcards (*, ?, [...]) in the source pattern and uses numbered wildcards (#1, #2, etc.) in the target pattern to represent the text matched by the corresponding wildcard in the source.

This makes it ideal for tasks such as changing file extensions, inserting text into filenames, or reordering parts of a filename across a large set of files. mmv also offers a dry-run mode (-n) to preview operations before execution, significantly reducing the risk of unintended file changes. It can prevent accidental overwrites and handles various file types, making it an indispensable tool for advanced file management in Unix-like systems.

CAVEATS

mmv's wildcard patterns are interpreted internally by the command itself, not by the shell. Therefore, it is almost always necessary to enclose both the source_pattern and target_pattern in single quotes to prevent the shell from expanding them prematurely. Errors in pattern matching can lead to unintended file changes; always use the -n (dry-run) option first for complex operations.

WILDCARD SYNTAX

mmv uses a specific set of wildcards in its source_pattern:
*: Matches any string of zero or more characters.
?: Matches any single character.
[...]: Matches any single character within the specified set (e.g., [abc], [0-9]).
_: Matches the empty string. This is typically used to ensure a specific part of a pattern is present but matches nothing.

For the target_pattern, mmv uses numbered wildcards like #1, #2, etc., which correspond to the text matched by the 1st, 2nd, etc., wildcard (*, ?, [...]) in the source_pattern. For example, '*.txt' '#1.bak' would rename 'file.txt' to 'file.bak'.

HISTORY

mmv is a long-standing utility in the Unix/Linux ecosystem, part of the mmv package. It was designed to provide robust pattern-based file manipulation, often predating or complementing other tools like the Perl-based rename utility. Its unique '#' wildcard syntax for capturing and re-using matched parts of filenames distinguishes it from standard shell globbing or regular expressions.

SEE ALSO

mv(1), cp(1), ln(1), rename(1)

Copied to clipboard