Renaming files is a common task for Linux users, whether for better organization or processing files in bulk. While tools like mv
and rename
are useful, mmv
(short for multiple move) stands out for its ability to handle batch file renaming with ease and flexibility.
This article will introduce you to mmv
, show you how to install it, and provide examples of advanced file-renaming scenarios.
What is mmv
?
mmv
is a command-line utility for renaming, moving, and copying multiple files simultaneously. Unlike the mv
command, which operates on a single file at a time, mmv
simplifies bulk operations by using patterns and placeholders.
Installing mmv
on Linux
The installation process for mmv
depends on your Linux distribution. Use the corresponding command below:
sudo apt install mmv # For Debian, Ubuntu, Mint
sudo yum install mmv # For RHEL, CentOS, Fedora, Rocky/AlmaLinux
sudo emerge -a sys-apps/mmv # For Gentoo Linux
sudo apk add mmv # For Alpine Linux
sudo pacman -S mmv # For Arch Linux
sudo zypper install mmv # For OpenSUSE
sudo pkg install mmv # For FreeBSD
Once installed, verify the installation by running:
mmv -h
Basic Syntax of mmv
The general format of the mmv
command is:
mmv [options] source_pattern target_pattern
source_pattern
: Matches the files you want to rename.target_pattern
: Specifies how the renamed files should appear.- Placeholders (
#1
,#2
): Correspond to parts of the filename matched by wildcards.
For example:
mmv '*.txt' '#1.md'
This renames all .txt
files to .md
files while preserving the filenames.
Advanced Usage Examples
Here are several examples that demonstrate the versatility of mmv
:
1. Rename Files with a Pattern
Suppose you have files named file1.txt
, file2.txt
, file3.txt
, and you want to rename them to document1.txt
, document2.txt
, etc.
Command:
mmv 'file*.txt' 'document#1.txt'
file*.txt
: Matches files starting withfile
and ending in.txt
.document#1.txt
: Renames matched files by replacingfile
withdocument
.
2. Add a Prefix or Suffix to Filenames
Add a Prefix
For files like image1.jpg
, image2.jpg
, you can add the prefix 2025_
:
mmv '*.jpg' '2025_#1.jpg'
This renames the files to 2025_image1.jpg
, 2025_image2.jpg
, etc.
Add a Suffix
To add _2025
as a suffix:
mmv '*.jpg' '#1_2025.jpg'
Resulting filenames: image1_2025.jpg
, image2_2025.jpg
.
3. Rename Files Using Regular Expressions
If you have files like data_01.txt
, data_02.txt
, and you want to remove the leading zero in the numbering:
mmv 'data_0*.txt' 'data_#1.txt'
Here, #1
retains the numeric part of the filename after removing the leading zero.
4. Change File Extensions
To rename all .txt
files to .csv
:
mmv '*.txt' '#1.csv'
This replaces .txt
with .csv
while keeping the original filenames.
5. Recursive Renaming in Subdirectories
To rename all .txt
files in the current directory and its subdirectories:
mmv -r '*.txt' '#1.md'
The -r
option ensures that mmv
searches subdirectories recursively.
6. Replace Specific Parts of Filenames
If you have files like report_Jan.pdf
, report_Feb.pdf
and want to replace report_
with summary_
:
mmv 'report_*.pdf' 'summary_#1.pdf'
This renames the files to summary_Jan.pdf
, summary_Feb.pdf
.
Tips for Safe Renaming
- Preview Changes: Before executing, use the
-n
option to preview the changes without applying them:bashCopiar códigommv -n '*.txt' '#1.md'
- Backup Files: If you’re working on critical files, ensure you have a backup before bulk renaming.
- Avoid Overwrites: Use unique target patterns to prevent accidentally overwriting files.
Conclusion
mmv
is a powerful and flexible tool for bulk file renaming in Linux, allowing you to handle complex patterns, add prefixes/suffixes, or modify extensions with ease. Whether you’re a seasoned user or new to Linux, mmv
can save you significant time when organizing and managing files.