Frage

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
War es hilfreich?

Lösung

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

Andere Tipps

You have a stray slash.

sed's basic regular expressions need lots of backslashes. Try one of

mv "$i" "$(echo "$i" | sed -r 's_Photo ([0-9]+)-([0-9]+)-([0-9]+)_\3-\2-\1_')"
mv "$i" "$(echo "$i" | sed 's_Photo \([0-9]\+\)-\([0-9]\+\)-\([0-9]\+\)_\3-\2-\1_')"

Note you don't have to capture the end of the line just to refer to it unchanged.


Also the ending double quote at the end of the line is not a plain double quote:

$ od -c <<< '  mv "$i" "`echo $i | sed '\''s_Photo ([0-9]+)-([0-9]+)-([0-9]+) (.*)_\3-\2-\1 \4_/'\''`”'
0000000           m   v       "   $   i   "       "   `   e   c   h   o
0000020       $   i       |       s   e   d       '   s   _   P   h   o
0000040   t   o       (   [   0   -   9   ]   +   )   -   (   [   0   -
0000060   9   ]   +   )   -   (   [   0   -   9   ]   +   )       (   .
0000100   *   )   _   \   3   -   \   2   -   \   1       \   4   _   /
0000120   '   ` 342 200 235  \n
0000126
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top