How to Use the Command 'mmv' (with Examples)
The mmv
command is a powerful utility used to move, rename, or copy files in bulk. It allows users to perform complex file operations quickly and efficiently using a pattern-based approach. This can save a significant amount of time and effort when working with numerous files that need to be managed in a consistent way. The command is particularly useful in scripting and batch operations, making it a valuable tool for system administrators and developers.
Use Case 1: Rename All Files with a Certain Extension to a Different Extension
Code:
mmv "*.old_extension" "#1.new_extension"
Motivation:
Renaming file extensions from one type to another is a common task, especially when dealing with software projects or data conversions that require a specific file type. Imagine you have a directory filled with configuration files ending in .conf
and you want to change them all to .cfg
to comply with a new system requirement. Manually renaming each file would be tedious and prone to error, but with mmv
, this task becomes quick and painless.
Explanation:
*.old_extension
: This wildcard pattern matches all files with the specified old extension. The asterisk*
acts as a placeholder for any number of characters before the extension.#1.new_extension
: The#1
is a placeholder that references the portion of the filename matched by*
. The command replaces the old extension with the new one while keeping the filename intact.
Example Output:
Before using the command, you have files like document1.old_extension
, document2.old_extension
, etc. After executing the command, they become document1.new_extension
, document2.new_extension
, etc.
Use Case 2: Copy report6part4.txt
to ./french/rapport6partie4.txt
Along With All Similarly Named Files
Code:
mmv -c "report*part*.txt" "./french/rapport#1partie#2.txt"
Motivation:
When working with files that follow a particular naming convention, it’s often necessary to not only rename but also re-organize them into different directories for better structure or localization. Suppose you have a series of reports segmented by parts and chapters, and you need to localize their filenames into French while also organizing them into a specific directory. This command helps automate the repetitive process, alleviating human error and saving time.
Explanation:
-c
: This option copies files instead of moving them."report*part*.txt"
: This pattern selects files that match the naming convention of a report (e.g.,report6part4.txt
)."./french/rapport#1partie#2.txt"
: The target pattern where#1
represents the first wildcard match (*
afterreport
), and#2
the second (*
afterpart
). This maps the original filename structure into the new localized format.
Example Output:
From files such as report6part4.txt
, report9part1.txt
, etc., in your working directory, they will be copied as ./french/rapport6partie4.txt
, ./french/rapport9partie1.txt
, etc.
Use Case 3: Append All .txt
Files Into One File
Code:
mmv -a "*.txt" "all.txt"
Motivation:
Combining multiple text files into a single file is a common requirement, especially when gathering data logs or documentations that are split across multiple files. This operation is beneficial for creating a consolidated view or backup of text data without manually opening and copying each file’s content. mmv
simplifies this aggregation by appending the content seamlessly.
Explanation:
-a
: This flag appends the contents of each source file to the target file."*.txt"
: A wildcard pattern matching all text files in the current directory."all.txt"
: The destination file where all contents will be appended. It will either create a new file if it does not exist or add to the end of an existing file.
Example Output:
All individual text files such as file1.txt
, file2.txt
, etc., are merged into a single file all.txt
, with their contents sequentially appended inside.
Use Case 4: Convert Dates in Filenames from “M-D-Y” Format to “D-M-Y” Format
Code:
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"
Motivation:
Renaming files to reflect a different date format might be necessary for consistency with international standards or compliance with organizational naming conventions. Changing a U.S. date format (Month-Day-Year) to a European format (Day-Month-Year) can help prevent misunderstandings among a global team. This command provides an efficient way to make this conversion accurately across all relevant files.
Explanation:
"[0-1][0-9]-[0-3][0-9]-[0-9][0-9][0-9][0-9].txt"
: The pattern recognizes date format where month and day are comprised of two digits each, separated by-
, followed by a four-digit year, e.g.,11-05-2023.txt
."#3#4-#1#2-#5#6#7#8.txt"
: Here,#3#4
swaps to position where the day is in the original filename and#1#2
where the month resided. This rearranges the order toDD-MM-YYYY
.
Example Output:
A file named 11-05-2023.txt
would be renamed to 05-11-2023.txt
, altering the format from Month-Day-Year to Day-Month-Year.
Conclusion:
The mmv
command provides substantial utility for batch file renaming, moving, copying, and content appending, transforming complex file management tasks into simple command-line operations. Its pattern-based approach significantly reduces manual effort and errors in handling large numbers of files, making it an essential tool for anyone working extensively with file systems.