Table of Contents#
- Installation
- Basic Syntax
- Simple Renaming Examples
- Using Wildcards
- Advanced Renaming with Regular Expressions
- Renaming Files in Subdirectories
- Common Pitfalls and How to Avoid Them
- Conclusion
- References
Installation#
On Linux#
- Debian/Ubuntu:
You can install
mmvusing the package manager. Open the terminal and run:
sudo apt-get install mmv- Fedora:
sudo dnf install mmvOn macOS#
You can use Homebrew. First, make sure Homebrew is installed. Then run:
brew install mmvBasic Syntax#
The basic syntax of mmv is:
mmv 'old_pattern' 'new_pattern'Here, old_pattern is the pattern that matches the names of the files you want to rename, and new_pattern is the pattern that defines how the files should be renamed.
Simple Renaming Examples#
Let's say you have a set of files named file1.txt, file2.txt, file3.txt, etc. And you want to rename them to document1.txt, document2.txt, document3.txt. You can use mmv like this:
mmv 'file*' 'document#1'In this example, the * in file* is a wildcard that matches any characters after file. The #1 in document#1 means that the part of the old file name that matched the first wildcard (in this case, the number after file) will be inserted into the new file name.
Using Wildcards#
* (Asterisk)#
The * wildcard matches any sequence of zero or more characters. For example, if you have files like image_2023-01-01.jpg, image_2023-02-01.jpg, and you want to rename them to photo_2023-01-01.jpg, photo_2023-02-01.jpg, you can do:
mmv 'image_*' 'photo_#1'? (Question Mark)#
The ? wildcard matches exactly one character. Suppose you have files a1.txt, a2.txt, b1.txt, b2.txt and you want to rename the ones starting with a to alpha1.txt, alpha2.txt. You can use:
mmv 'a?' 'alpha#1'Advanced Renaming with Regular Expressions#
mmv also supports regular expressions for more complex renaming. For example, if you have files named user_1234.log, user_5678.log and you want to extract the number part and rename them to log_1234, log_5678. You can use:
mmv -r 'user_([0-9]+).log' 'log_$1'Here, the -r option tells mmv to use regular expressions. The ([0-9]+) is a capturing group that matches one or more digits. The $1 in the new pattern refers to the content of the first capturing group.
Renaming Files in Subdirectories#
If you want to rename files in subdirectories, you can use the -x option (recursive). For example, if you have a directory structure like:
parent/
├── subdir1/
│ └── oldfile1.txt
└── subdir2/
└── oldfile2.txt
And you want to rename oldfile1.txt to newfile1.txt and oldfile2.txt to newfile2.txt in their respective subdirectories. You can run:
mmv -x '*/oldfile*' '*/newfile#1'Common Pitfalls and How to Avoid Them#
- Overwriting Files: Make sure the new file names don't already exist. You can use the
-noption to do a dry run (simulate the renaming without actually changing the files) first. For example:
mmv -n 'old*' 'new*'- Incorrect Wildcard/Regex Usage: Double-check your patterns. Test with a small set of files if possible. If you're using regular expressions, make sure you understand how capturing groups work.
Conclusion#
mmv is a powerful tool for advanced file renaming. Whether you're dealing with simple wildcard-based renaming or complex regular expression operations, it can handle a wide variety of scenarios. With the knowledge of its installation, syntax, and different features like handling subdirectories, you can efficiently rename files in your system.