The mmv
command is a versatile utility for moving, renaming, copying, or linking multiple files by matching filename patterns. It simplifies batch file operations that would otherwise require loops or more complex scripting.
Install mmv
(if not pre-installed)
# Debian/Ubuntu
sudo apt install mmv
# Fedora
sudo dnf install mmv
# macOS (via Homebrew)
brew install mmv
Basic Syntax
mmv [options] 'source_pattern' 'target_pattern'
Use single quotes ('
) to prevent shell expansion of wildcards like *
or ?
.
Common Use Cases
1. Rename Files with a Pattern
Replace all .txt
extensions with .md
:
mmv '*.txt' '#1.md'
#1
captures the wildcard match from the source pattern (e.g.,file1.txt
→file1.md
).
2. Add a Prefix to Filenames
Add backup_
to all .jpg
files:
mmv '*.jpg' 'backup_#1.jpg'
- Example:
photo.jpg
→backup_photo.jpg
.
3. Change Filename Case
Convert all .JPG
filenames to lowercase:
mmv '*.JPG' '#1.jpg' -l
- Use
-l
for lowercase or-u
for uppercase.
4. Replace Substrings
Replace underscores _
with hyphens -
in filenames:
mmv '*_*' '#1-#2'
- Example:
report_2023.txt
→report-2023.txt
.
5. Sequential Numbering
Rename all .png
files to image_1.png
, image_2.png
, etc.:
mmv '*.png' 'image_#1.png' -n 1
-n 1
starts numbering at1
.
6. Copy Files (Instead of Move)
Copy all .conf
files to .conf.bak
:
mmv -c '*.conf' '#1.conf.bak'
-c
: Copy instead of move.
7. Delete Substrings
Remove temp_
from filenames:
mmv 'temp_*' '#1'
- Example:
temp_data.csv
→data.csv
.
Advanced Options
- Dry Run: Preview changes without executing (
-n
):
mmv -n '*.log' '#1.log.bak'
- Verbose Mode: Show detailed output (
-v
):
mmv -v '*.txt' '#1.md'
- Symbolic Links: Create links instead of moving (
-l
):
mmv -l '*.sh' 'scripts/#1'
Handling Spaces/Special Characters
Use \
to escape spaces or special characters:
mmv 'file\ with\ spaces*.txt' 'new_#1.txt'
Caveats
mmv
overwrites files by default. Use-n
to test first.- For more complex regex, consider the
rename
command (Perl-based).
Alternatives
rename
(Perl):
rename 's/\.jpeg$/\.jpg/' *.jpeg
find
+xargs
:
find . -name "*.txt" -print0 | xargs -0 -I {} mv {} {}.bak