Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 800
Neilvert Noval Avatar asked Jan 17 '11 07:01

Neilvert Noval


People also ask

How do I batch rename a JPEG?

You can batch rename images in Windows by selecting (Shift+click or Ctrl+click to select several files; Ctrl+A to select all) and pressing right-click > "Rename". Your file names will look like image (1), image (2), image (3) etc.


2 Answers

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.

like image 190
Theo Band Avatar answered Sep 30 '22 05:09

Theo Band


If you're working with JPG that contains EXIF data (ie. from digital camera), then you can use following to get the creation date instead of stat.

exif -t 0x9003 -m Image0100.jpg 

Per request, here's the command and output. A couple of points to note:

  • Since not every file has exif data, we want to check that dst is valid before doing the rest of commands.
  • The output from exif has a space, which is a PITA for filenames. Use sed to replace with '-'.
  • Note that I use 'echo' before the mv to test out my scripts. When you're confident that it's doing the right thing, then you can remove the 'echo'... you don't want to end up like the guy that got all the files blown away.

Command

for i in *.jpg; do   dst=$(exif -t 0x9003 -m $i ) &&   dst_esc=$(echo $dst | sed 's/ /-/g' ) &&   echo mv $i $dst_esc.jpg done 

Output

'12379632.jpg' does not contain tag 'DateTimeOriginal'. mv 15084688.jpg 2003:02:28-21:48:54.jpg mv 15136312.jpg 2003:03:01-10:36:05.jpg mv 15137960.jpg 2003:03:01-10:36:38.jpg mv 15140744.jpg 2003:03:01-10:37:46.jpg 
like image 41
fseto Avatar answered Sep 30 '22 06:09

fseto