How to use the command mmv (with examples)

How to use the command mmv (with examples)

The mmv command is a powerful tool that allows users to move and rename files in bulk. It makes file management tasks much faster and more efficient. This command supports a range of patterns and substitutions, allowing for complex file renaming and moving operations.

Use case 1: Rename all files with a certain extension to a different extension

Code:

mmv "*.old_extension" "#1.new_extension"

Motivation: This use case is particularly useful when you have a large number of files with a specific file extension that you want to change. Instead of manually renaming each file, the mmv command allows you to do it in a single line.

Explanation:

  • *.old_extension: This is a glob pattern that matches all files with the extension old_extension.
  • #1.new_extension: This is the substitution pattern used to rename the files. The #1 represents the matched part of the glob pattern before the . (i.e., the file name without the extension), and new_extension is the new extension you want to set.

Example output: If you have files like file1.old_extension, file2.old_extension, and file3.old_extension, running the command mmv "*.old_extension" "#1.new_extension" will rename them to file1.new_extension, file2.new_extension, and file3.new_extension.

Use case 2: Copy files with similar names to a different directory with modified names

Code:

mmv -c "report*part*.txt" "./french/rapport#1partie#2.txt"

Motivation: When you want to copy files with similar names to a different directory, the mmv command simplifies the process. It also allows you to modify the names of the copied files based on certain patterns.

Explanation:

  • -c: This option specifies that the files should be copied rather than moved.
  • "report*part*.txt": This is a glob pattern that matches files with names starting with “report,” followed by any characters, “part,” and any characters before the “.txt” extension.
  • "./french/rapport#1partie#2.txt": This is the destination path for the copied files. The #1 and #2 in the destination pattern represent the parts of the original file name that matched the corresponding parts in the glob pattern.

Example output: Suppose you have files like report1part2.txt, report2part3.txt, and report3part4.txt. Running the command mmv -c "report*part*.txt" "./french/rapport#1partie#2.txt" will copy them to the directory ./french/ with modified names, resulting in files like ./french/rapport1partie2.txt, ./french/rapport2partie3.txt, and ./french/rapport3partie4.txt.

Use case 3: Append all .txt files into one file

Code:

mmv -a "*.txt" "all.txt"

Motivation: When you have multiple text files and want to merge them into a single file for easier processing, the mmv command with the -a option provides a simple solution.

Explanation:

  • -a: This option tells mmv to append the contents of the source files into the target file.
  • "*.txt": This glob pattern matches all files with the extension .txt.
  • "all.txt": This is the target file where all the contents of the matched files will be appended.

Example output: If you have files like file1.txt, file2.txt, and file3.txt, running the command mmv -a "*.txt" "all.txt" will append the contents of these files into all.txt, resulting in a single file all.txt that contains the combined contents of the original files.

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: In some cases, you may need to convert the format of dates in filenames. This use case demonstrates how you can modify the file names using patterns to change the date format.

Explanation:

  • [0-1][0-9]-[0-3][0-9]-[0-9][0-9][0-9][0-9].txt: This glob pattern matches files with names in the format “M-D-Y.txt”.
  • #3#4-#1#2-#5#6#7#8.txt: This substitution pattern rearranges the parts of the matched filename to convert it from “M-D-Y” format to “D-M-Y” format.

Example output: Suppose you have files like 01-15-2022.txt, 10-05-2022.txt, and 12-31-2022.txt. Running the command 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" will rename them to 15-01-2022.txt, 05-10-2022.txt, and 31-12-2022.txt, respectively, changing the date format from “M-D-Y” to “D-M-Y”.

Related Posts

How to use the command 'git psykorebase' (with examples)

How to use the command 'git psykorebase' (with examples)

Git psykorebase is a command that allows users to rebase a branch on top of another using a merge commit with only one conflict handling.

Read More
How to use the command pnmcolormap (with examples)

How to use the command pnmcolormap (with examples)

The pnmcolormap command is used to create a quantization color map for a PNM (Portable aNyMap) image.

Read More
sd (with examples)

sd (with examples)

Use Case 1: Trim whitespace using a regular expression Code: echo 'lorem ipsum 23 ' | sd '\s+$' '' Motivation: Sometimes, when working with text, there may be extra whitespace at the end of a line that needs to be removed.

Read More