How to batch rename files in Mac OS X?

Untested:

for f in *.mpg; do
  mv "$f" "$(stat -f "%m" -t "%Y %m %d %H %M %S" "$f") $f"
done

stat -f "%m" should produce the last modified date of the file, -t "%Y %m %d %H %M %S" should format the date according to your requirements.


The original question specifies that the resultant files also end with a file extension. To do this in a portable fashion, in bash, grab the extension from the /path/to/file.ext with ${f##*.} built-in, resulting in a one-liner of:

for f in *.FOO; do mv "$f" "$( /usr/bin/stat -f "%Sm" -t "%Y%m%d_%H%M%S" ).${f##*.}" ; done

and a code block of

for f in *.FOO ; do
    mv "$f" "$( /usr/bin/stat -f "%Sm" -t "%Y%m%d_%H%M%S" ).${f##*.}"
done

so when you change FOO (eg from *.mpg to *.png) the output filenames are sane.