How to rename hundreds of files by changing the date format in the file name

I have hundreds of files that are named “mm-dd-yyyy text”. I would like to rename them “yyyy-mm-dd same text”. An example is “03-21-2023 packing list” which I would like to rename “2023-03-21 packing list”. Please note that the text is unique to the file.

Any help would be deeply appreciated.

This is something you can do easily with terminal and some basic regex. If you want a GUI, I think A Better Finder Rename will do this for you. Apple’s own bulk-changing feature might not be quite powerful enough.

Try the following in the Terminal (prints results, makes no changes):

for f in *; do echo "${f//(\d{2})-(\d{2})-(\d{4})(.*)/$3-$1-$2$4/}"; done

If you are happy with the results:

for f in *; do mv -n "$f" "${f//(\d{2})-(\d{2})-(\d{4})(.*)/$3-$1-$2$4/}"; done
2 Likes

This is the use case for Hazel.

A Hazel rule to do this could look like this:

Importantly, the first mention of “Old Date Format” in the trigger area would be formatted like this:

But in the second mention of “Old Date Format” in the action area, you would use the Old Date Format token, but re-sort it, like this:

1 Like

A bit of a philosophical point perhaps, but…

Hazel is based around ongoing monitoring and repeated application of one or more rules. I would say that this would only be “the” use case if, and only if, you wish to do it in a repeatedly with an automated trigger.

In terms of being a useful tool and more approachable, then I absolutely agree. Hazel has a good GUI and documentation to help you accomplish this. I’ve been a Hazel user myself since version 2 - it is an invaluable tool.

Command line renaming of files … well that really is totally in the wheelhouse of the shell and has been for many decades now. As a one-off renaming, I would argue that it is technically a better match than Hazel and “the” use case fits more to that.

However, when you hit the limits of Hazel’s native options, which I have done a number of times, Hazel always allows you to fall back and use shell commands to interact with your files, bringing all that file management of the terminal to the tool. If you are really working Hazel hard and bumping into limits I would strongly recommend brushing up on what the command line offers you to bolster both your ad hoc and ongoing file management requirements.

Points taken across the board, as usual, you are correct @sylumer! Especially the point about a recurring vs. one-off need.

By the use case, I was referring to the bulk renaming of date-based file names as a core competency of Hazel, not that it was the only/absolute best tool for the job. I could have said that better. I have been working on moving meeting notes from Drafts to Obsidian and have been deep in some Hazel rules, so I enthusiastically chimed in without nuance.

Considering this is @gtd_nerd’s first post to the forum and the details of their question. I assumed a GUI tool like Hazel might be more approachable than a terminal command. Even though the terminal command is free, fast, and already written, the Terminal can be intimidating.

The litany of resources and methods for Mac automation never ceases to excite and intimidate me; thanks for pointing out the benefits of the command line and its use with Hazel!