linuxunixcommand-line

Rename JPG files according to date created


I want to rename all files in a certain directory. Renaming them to their date of creation.
So if my file is Image1.jpg, it should rename into something like "Jan 16 12:09:42 2011.jpg"
I want to do this through command line. I've been trying:

stat -f %SB Image0100.jpg

But how can I combine this with mv command? And how will I iterate stat and mv through the whole files?
Or are there simple ways to rename all files with their date creation?


Solution

  • jhead -n DSCN0382.JPG
    DSCN0382.JPG --> 0408-150734.jpg
    

    any strftime argument can be given as well:

    jhead -n%Y%m%d-%H%M%S *.jpg 
    

    This will rename files matched by *.jpg in the format YYYYMMDD-HHMMSS

    jhead -n%Y%m%d-%H%M%S DSCN0382.JPG
    DSCN0382.JPG --> 20120408-150734.jpg
    

    see also the man page for lots of other cool options. You can for instance correct (shift) the EXIF date. This is very handy when merging files from different camera's when some camera's have an incorrect time set.