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