Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the bunch of files that have names like MOV001.mpg, MOV002.mpg etc. They all have different date-time.

How can I rename them so they turn into 2012 08 05 13 45 33 MOV001.mpg, 2012 09 02 23 45 07 MOV002.mpg etc.?

In other words, how can I extract the date and time information from the files I rename?

Thank you much in advance.

share|improve this question
up vote 1 down vote accepted


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

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.

share|improve this answer
Thank you. Helped when using stat -f "%Sm" instead of stat -f "%m" – Sunny Reborn Pony Nov 15 '12 at 9:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.