Search code examples
regexmacoscommand-linesedbatch-rename

OSX: change date format in multiple file names


I have a large number of files in this format (iPhone camera):

Photo 31-12-13 12 59 59.jpg

How can I batch rename these files using the OSX command line to this (ISO) format:

2013-12-31 12 59 59.jpg

I have tried using the command below, but it doesn't seem to work:

for i in Photo*
do
  mv "$i" "`echo $i | sed 's_Photo ([0-9]+)-([0-9]+)-([0-9]+) (.*)_\3-\2-\1 \4_/'`”
done

Solution

  • You can use:

    for i in Photo*; do
        mv "$i" "$(sed -E 's/^Photo ([0-9]*)-([0-9]*)-([0-9]*) (.*)$/20\3-\2-\1 \4/' <<< "$i")"
    done