Categories
terminal

Renaming files in bulk: mmv

There are many ways to rename multiple files at once. One of the easiest ones, if you have the power to install new packages or if you already have it installed in your system, is with ‘mmv’. Here are some examples. We’ll use the following file structure:

$ ls
develop-apple.config develop-jackfruit.config
develop-banana.config develop-orange.config

To replace the prefix ‘develop’ with ‘test’ from filenames that follow the pattern “develop-banana.config”. First use the ‘-n’ flag for ‘no-execute’ mode. It’s like a ‘dry-run’, it’ll show the changes about to be made, without executing the changes:

$ mmv -n 'develop*' 'test#1'
develop-apple.config -> test-apple.config
develop-banana.config -> test-banana.config
develop-jackfruit.config -> test-jackfruit.config
develop-orange.config -> test-orange.config

To replace a suffix:

$ mmv -n '*develop*' '#1test#2'
apple-develop.json -> apple-test.json
banana-develop.json -> banana-test.json
jackfruit-develop.json -> jackfruit-test.json
orange-develop.json -> orange-test.json

To replace a file extension:

$ mmv -n '*.json' '#1.config' apple-develop.json -> apple-develop.config banana-develop.json -> banana-develop.config jackfruit-develop.json -> jackfruit-develop.config orange-develop.json -> orange-develop.config

Don't forget to remove the '-n' flag when you're 100% sure of the changes you're making so they're written to disk.