SocraticDev logo

Massively renaming files

Sunday July 12th 2020 by SocraticDev

Command-line tool mmv moves (or copies, appends, or links, as specified) each source file matching a from pattern to the target name specified by the to pattern. This multiple action is performed safely, i.e. without any unexpected deletion of files due to collisions of target names with existing filenames or with other target names. Furthermore, before doing anything, mmv attempts to detect any errors that would result from the entire set of actions specified and gives the user the choice of either proceeding by avoiding the offending parts or aborting.

Use case: renaming dozens of files manually is long and inhuman

It is not every day that you feel the need to rename files massively. However, in a rapid development process, standardizing the name of generated files is not top priority. For example, as part of my current project, I developed a light script to download and time stamp web pages on a daily basis: https://github.com/socraticDevBlog/webpage-grabber. During development, I improved the file names' syntax. As a result, the names of the files downloaded at the start of the project differ from the most recent.

The second phase of the project will be to scrape (web scraping) the content of downloaded web pages to extract the content and insert it into a database. For the web scraping application to be able to drink from a given directory, the names of the files must be standardized there. As the process of processing the data acquired will be automated, all the files must be named consistently.

Initially, I thought that there would be no problem to rename these files. I proposed to use the native commands of linux like mv and grep. However, these commands apply to one file at a time (unless you script bash or perl loops). By searching a little, I found the mmv tool and following the instructions, I quickly standardize all my files to be processed.

# apt-get install mmv

$ man mmv

The first command installs mmv on your server (Debian, Ubuntu, and some other distros). The second displays the tool's instruction manual in terminal.

Good success !

Sources

mmv (1) - Linux Man Pages