Renaming files in Linux is something we all do, whether it’s to organize our files better or to rename files in bulk.
While there are basic tools like mv and rename, there’s an advanced tool called mmv
that makes the process much easier, especially when you need to rename multiple files at once.
As an experienced Linux user, I’ve found mmv
to be a powerful tool for batch renaming files, and in this post, I’ll show you how to use it effectively.
What is mmv?
mmv
stands for multiple move, which is a command-line utility that allows you to rename, move, and copy multiple files at once. Unlike the mv
command, which is great for renaming one file at a time, mmv
is designed to handle bulk renaming with ease.
To install mmv
on Linux, use the following appropriate command for your specific Linux distribution.
sudo apt install mmv [On Debian, Ubuntu and Mint] sudo yum install mmv [On RHEL/CentOS/Fedora and Rocky/AlmaLinux] sudo emerge -a sys-apps/mmv [On Gentoo Linux] sudo apk add mmv [On Alpine Linux] sudo pacman -S mmv [On Arch Linux] sudo zypper install mmv [On OpenSUSE] sudo pkg install mmv [On FreeBSD]
Once installed, you’re ready to start renaming your files.
The basic syntax of mmv
is:
mmv [options] source_pattern target_pattern
source_pattern
: This is the pattern that matches the files you want to rename.target_pattern
: This is how you want the renamed files to appear.
For example, if you want to rename all .txt
files to .md
files, you would use:
mmv '*.txt' '#1.md'
Here, #1
refers to the part of the filename matched by the *
wildcard.
Examples of Using mmv for Advanced Renaming in Linux
Here are some advanced examples of how to use mmv
effectively:
1. Renaming Multiple Files with a Pattern
Let’s say you have several files like file1.txt, file2.txt, file3.txt, and so on and you want to rename them to document1.txt, document2.txt, document3.txt, etc.
Here’s how you can do it:
mmv 'file*.txt' 'document#1.txt'
In this example:
file*.txt
matches all files starting withfile
and ending with.txt
.document#1.txt
renames them todocument1.txt
,document2.txt
, etc.
2. Renaming Files by Adding a Prefix or Suffix
Let’s say you want to add a prefix or suffix to a group of files. For example, you have files like image1.jpg, image2.jpg, image3.jpg, and you want to add the prefix 2025_
to each file.
Here’s how you do it:
mmv '*.jpg' '2025_#1.jpg'
This will rename the files to 2025_image1.jpg, 2025_image2.jpg, etc.
If you wanted to add a suffix instead, you could use:
mmv '*.jpg' '#1_2025.jpg'
This will rename the files to image1_2025.jpg, image2_2025.jpg, etc.
3. Renaming Files with Regular Expressions
mmv
supports regular expressions, so you can use them to match complex patterns. For example, let’s say you have files like data_01.txt, data_02.txt, data_03.txt, and you want to remove the leading zero in the numbers.
You can do this with:
mmv 'data_0*.txt' 'data_#1.txt'
4. Renaming Files in Subdirectories
If you have files in subdirectories and want to rename them as well, you can use the -r
option to rename files recursively. For example, if you want to rename all .txt
files in the current directory and all subdirectories:
mmv -r '*.txt' '#1.txt'
This will rename all .txt
files in the current directory and its subdirectories.
Conclusion
Renaming files in Linux doesn’t have to be a tedious task. With mmv
, you can easily rename multiple files with advanced patterns, saving you time and effort. Whether you need to add a prefix, change extensions, or rename files in bulk, mmv
has you covered.
Give it a try, and let me know how it works for you! If you have any questions or need further help, feel free to leave a comment below.