Batch File Renaming the Lazy Way

When I first converted my blog over to hugo from hakyll, I neglected to put the date published onto the front of the blog posts file names. This resulted in URLs that were slightly less descriptive than desired, and also made it harder to scan through the markdown files quickly. Luckily in each file is a header, and within each header is a date field. Therefore, with my rusty old bash trickery I was able to spit out the following one-liner to do the job of renaming all the files.

find . -type f \
       -exec sh -c 'mv "$1" $(grep "date =" "$1" \
            | cut -f3 -d\  \
            | tr \" \  \
            | sed "s/T.*$//" \
            | sed "s/ //g")-$(basename "$1")' \
          sh {} \;

So if you ever end up in a situation like me, the above magic spell will fix you right up. Now, I could have manually edited each and every file by hand, but where’s the fun in that? Why bother using computers at all if you still do all the boring stuff by hand?