Renaming files by their modification date
Mia Lopez
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.
34 Answers
Install exiftool
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.
6You 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; doneExample
% 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 2015Explanation
-iname "*.JPG"matches all JPGs case insensitive-print0and$'\0'your filenames can have spaces and newlines
from man stat
%Y time of last data modification, seconds since EpochIf your images have exif data, than you can follow the steps below.
Install exiftool
sudo apt-get install libimage-exiftool-perland 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 targetExplanation
-filename<ModifyDatemeans rename the image file using the image's modify date and time.-dmeans "Set format for date/time values".%y%m%d_%H%M%S%%-c.%%le, used in conjunction with-dspecifies 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%Smeans add the hour, minute, and second of the creation time, all represented by two digits.%%-cmeans 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..%%lemeans keep the original file name extension, but make it lower-case if it was originally upper-case, a nice option when cameras insist on usingJPGinstead ofjpg. (If you prefer upper-case extensions, then use .%%ue. If you prefer to keep the original case intact, use .%%e.)-ext jpgmeans only rename files with thejpgextension. To rename all image files in the source folder, don't specify any extensions.-rmeans "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".
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
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.HSTYou 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)%fto keep original filename