Hundreds of images need to be renamed with a terminal command using regex on MacOS 10.9 Mavericks.

The files are named this way:


They need to be renamed to this:

2013-11-24  10-12-45.jpg
2013-11-27  10-17-21.jpg
2013-11-28  10-34-23.jpg

Can anyone please show me how to do it?

  • 1
    Why do you want to use a regular expression?
    – Carl Norum
    Commented Nov 29, 2013 at 18:03
  • You might find the answer to this question a guide to what you want to do. stackoverflow.com/questions/13278391/…
    – KeithSmith
    Commented Nov 29, 2013 at 18:11
  • I just assumed that a regular expression would be the only way to do it. Now I know that I was wrong.
    – orangebreeze
    Commented Nov 29, 2013 at 18:33
  • Yeah, if its fixed format, probably don't need a regex. But, if badly formed filename sneaks in, a regex can give you more options for a recovery.
    – sln
    Commented Nov 29, 2013 at 19:15
  • Not complete, but I use something like: for a in *; do b=$(echo $a | perl -pe 's/^\d(\d\d)/\1/'); mv "$a" "$b"; done Commented Mar 13, 2014 at 14:46

2 Answers 2


You can try

for a in *.jpg ; do mv -- "$a" "${a:22:4}-${a:26:2}-${a:28:2} ${a:30:2}-${a:32:2}-${a:34:2}.jpg" ; done

A bit ugly, but it should get the job done. Like always, make sure to test on a copy before.

  • 1
    Are the ${a:22:4} substrings? start at character 22 and pull 4 characters?
    – JDiPierro
    Commented Nov 29, 2013 at 18:11
  • 2
    Yes they are. Assuming, as the in example, that all strings are formatted the same way. Commented Nov 29, 2013 at 18:15
  • This works, I was able to embedd this into my Hazel.app folder action as intended. Thank you.
    – orangebreeze
    Commented Nov 29, 2013 at 19:33

If the first part of the filenames don't always have the same length, you can use to extract the string after the second _ and use the first 8 digits to construct your filename:

#!/usr/bin/env bash

for file in folder/*; do
    outName=$(awk -F"_" '{ print substr($3,1,4)"-"substr($3,5,2)"-"substr($3,7,2) }' <<< "${file}").jpg
    mv "${file}" "${outName}"

You must log in to answer this question.