Monthly Archives: January 2021

Shell script to re-format poor date format in filenames

Published / by Kevin / 1 Comment on Shell script to re-format poor date format in filenames

New, more-good version here: New script to re-write dates in file names

After testing and some using, this is going to change to just write the mv statements to a file and to accept an argument to run them instead. No logging since that will serve the same purpose. This post does not contain the final version. It’s here for the sake of history.

And boom. Or so I hope. This *should* handle dates as “01.02.03” and the equivalent “01.02.2003”.
To-do: Check for administrator privileges or just write the log to ~/. MacOS (nee OS X (nee Mac OS)) has a default user log directory, but GNU does not. I suppose we could check for “~/.log/” and mkdir if false…

Title explains it pretty well. Uses egrep to find date, sed to edit it, mv to rename, and read for user confirmation prompt. Which, btw, can’t be done if you are already doing a read without feeding the new read from somewhere other than stdin. Hence the < /dev/tty at that point in the script. So many strangers to thank for sharing their knowledge online. Thanks, strangers! That should do it.

Why this exists is I had a co-worker who added dates to tons of files in the format “01.02.03”, which resulted in files named “Example filename 01.02.03.xmpl” Same for phone numbers. Clearly this is wrong and something must be done about it (now that new ones won’t be cropping up since You may be asking yourself how he got away with not getting an e-mailed file bounced back for multiple extensions? My educated guess is that he did and kept doing it the dumb way despite that.

Corrects the above format to “+%Y-%m-%d” (YYYY-MM-DD).

This could probably be done with fewer lines or overall be more betterer, but this is like my first whole shell script. Maybe my years of AppleScript are showing. *meh*
Continue reading