2

I've got some (96) images, that I need to rename. Every program or script I've used is automatically sorting files by their old name, but they need to be numbered in modification date order.

For example, an image has been taken on 11 V 2015 08:13:40 and is named f67546288.jpg, and a second has been taken on 11 V 2015 08:13:46, and is named f67553376.jpg.

I don't want the images ordered alphabetically, but by the time they were taken.

3
  • what would be the new name? is this something you could use? askubuntu.com/a/583355/72216 Jun 13, 2015 at 21:01
  • 2
    Please edit your question and show us the exact name of your images and the exact name you want them to be renamed to.
    – terdon
    Jun 14, 2015 at 11:28
  • 1
    "Renaming files by their modification date" != "by time, when the photo was taken"
    – A.B.
    Jun 14, 2015 at 15:43

4 Answers 4

3
  1. Install exiftool

  2. Use it to rename files according to the exif information in the image. Example:

    for f in "$@"
    do
        exiftool -ext jpg -d %Y%m%d_%H%M%S%%+c.%%le "-filename<CreateDate" "$f"
    done
    

Adjust output file name format to suit.

6
  • Nice idea.... On testing some images from my camera I found just exiftool -d %Y%m%d_%H%M%S%%+c.%%le "-filename<ModifyDate" *jpg works well (for testing I used copies in case it all went wrong :) - I used ModifyDate as the CreateDate was for each image to 2002:12:08 12:00:00 - you can list suitable source tags for the images using exiftool -t ./IMG_20150304_085858877.jpg | grep -Ei 'date|time', as images from different sources may have different tags
    – Wilf
    Jun 13, 2015 at 21:36
  • --ext pdf? That make no sense.
    – A.B.
    Jun 14, 2015 at 11:24
  • @A.B. oh yeah - from the test I did it seemed to ignore it anyway :)
    – Wilf
    Jun 14, 2015 at 12:23
  • 1
    @terdon regarding "--ext pdf": 'man exiftool' contains the following: -ext EXT, --ext EXT (-extension) Process only files with (-ext) or without (--ext) a specified extension. [...]. For example: [...] exiftool -ext "*" --ext xml DIR # process all but XML files
    – DaveEmme
    Jun 14, 2015 at 15:27
  • 1
    @terdon Your edit makes it clearer; the "--ext pdf" is a side issue. I just copied that from an old Mac OSX script I wrote years ago; I wanted to make sure I processed foo.jpg, foo.JPEG, and similar variations, but not any PDFs (which exiftool will modify).
    – DaveEmme
    Jun 14, 2015 at 15:49
2

You can use the modification time stamp in seconds since Epoch

Unix time (also known as POSIX time or erroneously as Epoch time) is a system for describing instants in time

find . -type f -iname "*.JPG" -print0 | while read -d $'\0' file; do mv "$file" "$(stat -c %Y "$file")".jpg; done

Example

% ls -og
total 2796
-rw-r--r-- 1 2859518 Jan  2 11:26 IMG_20150102_112628_8902.JPG

% find . -type f -iname "*.JPG" -print0 | while read -d $'\0' file; do mv "$file" "$(stat -c %Y "$file")".jpg; done

% ls -og                                                            
total 2796
-rw-r--r-- 1 2859518 Jan  2 11:26 1420194390.jpg

% date -d @1420194390
Fr 2. Jan 11:26:30 CET 2015

Explanation

  • -iname "*.JPG" matches all JPGs case insensitive
  • -print0 and $'\0' your filenames can have spaces and newlines

from man stat

%Y     time of last data modification, seconds since Epoch

If your images have exif data, than you can follow the steps below.

Install exiftool

sudo apt-get install libimage-exiftool-perl

and you can use the command below:

cd <your_images_path>
exiftool '-filename<ModifyDate' -d %y%m%d_%H%M%S%%-c.%%le -r -ext jpg .

Example

% ls -og
total 2808
-rw-r--r-- 1 2859518 Jan  2 11:26 IMG_20150102_112628_8902.JPG
drwxrwxr-x 3    4096 Jun 14 12:12 source
drwxrwxr-x 2    4096 Jun 14 12:12 target

% exiftool '-filename<ModifyDate' -d %y%m%d_%H%M%S%%-c.%%le -r -ext jpg .
    3 directories scanned
    1 image files updated
% ls -og                 

total 2808
-rw-r--r-- 1 2859518 Jan  2 11:26 150102_112628.jpg
drwxrwxr-x 3    4096 Jun 14 12:12 source
drwxrwxr-x 2    4096 Jun 14 12:12 target

Explanation

  • -filename<ModifyDate means rename the image file using the image's modify date and time.

  • -d means "Set format for date/time values".

  • %y%m%d_%H%M%S%%-c.%%le, used in conjunction with -d specifies the format to use for the date and time when renaming the file. Breaking the format down: -%y%m%d_ means the first part of the new file name should be composed of the last two digits of the creation-date year, followed by the month and day, both represented by two digits. The underscore _ means put in an underscore after the date part of the file name.

  • %H%M%S means add the hour, minute, and second of the creation time, all represented by two digits.
  • %%-c means that if two images have the same file name up to this point in the naming process, add "a copy number which is automatically incremented" to give each image a unique name. Note the doubled %% — necessary because of something called "escaping" that I don't fully understand. The - before the "c" isn't really necessary, but it puts a dash before the copy number.
  • .%%le means keep the original file name extension, but make it lower-case if it was originally upper-case, a nice option when cameras insist on using JPG instead of jpg. (If you prefer upper-case extensions, then use .%%ue. If you prefer to keep the original case intact, use .%%e.)
  • -ext jpg means only rename files with the jpg extension. To rename all image files in the source folder, don't specify any extensions. -r means "execute this command recursively for every image file in the top "source" folder (that is, the folder where all the files to be renamed are located), and also for the image files in all the source folder's subfolders, sub-subfolders, and so on".

Source

1

The classic way to rename files is to use mv and for loop. stat -c %y filename gives us time of last modification, which is the same as of file's creation. Stat has file birth date (%w), but it mostly fails, so %y is prefered

for file in *.png; do mv "$file" "$(stat -c %y "$file")".png; done

Simple solution, no software installation necessary, and does the job

0

Extracting modification time from EXIF

$ find -name '[0-9][0-9]*' -type f -exec jhead -ft -n%Y%m%d_%H%M%S_%f {} +
./DCIM/103OLYMP/P5074367.JPG
./DCIM/103OLYMP/P5074367.JPG --> ./DCIM/103OLYMP/20160507_103229_P5074367.jpg
Not JPEG: ./DCIM/103OLYMP/P5074374.ORF
./DCIM/103OLYMP/P5074374.JPG
./DCIM/103OLYMP/P5074374.JPG --> ./DCIM/103OLYMP/20160507_131746_P5074374.jpg
Not JPEG: ./DCIM/103OLYMP/P5074376.ORF
Not JPEG: ./DCIM/BACKUP.HST

You can also filter on JPEG/JPG extensions:

find -name '[0-9][0-9]*' -o -iname '*.jp*g' -type f -exec jhead -ft -n%Y%m%d_%H%M%S_%f {} +

Explanations:

  • -name '[0-9][0-9]*' to prevent renaming files already having a date prefix ('[0-9][0-9] to take into account years and months)
  • -iname '*.jp*g' to apply renaming only on JPG/JPEG extension files (can be skipped because jhead only processes supported files)
  • %f to keep original filename

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .