Naming based on file system date
In the linux shell:
for f in *.jpg
do
mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"
done
Explanation:
-
for f in *.jpg
doThis starts the loop over all jpeg files. A feature of this is that it will work with all file names, even ones with spaces, tabs or other difficult characters in the names.
-
mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"This renames the file. It uses the
-roption which tellsdateto display the date of the file rather than the current date. The specification+"%Y%m%d_%H%M%S"tellsdateto format it as you specified.The file name,
$f, is placed in double quotes where ever it is used. This assures that odd file names will not cause errors.The
-noption tomvtells move never to overwrite an existing file. -
doneThis completes the loop.
For interactive use, you may prefer that the command is all on one line. In that case, use:
for f in *.jpg; do mv -n "$f" "$(date -r "$f" +"%Y%m%d_%H%M%S").jpg"; done
Naming based on EXIF Create Date
To name the file based on the EXIF Create Date (instead of the file system date), we need exiftool or equivalent:
for f in *.jpg
do
mv -n "$f" "$(exiftool -d "%Y%m%d_%H%M%S" -CreateDate "$f" | awk '{print $4".jpg"}')"
done
Explanation:
The above is quite similar to the commands for the file date but with the use of exiftool and awk to extract the EXIF image Create Date.
-
The
exiftoolcommand provides the date in a format like:$ exiftool -d "%Y%m%d_%H%M%S" -CreateDate sample.jpg Create Date : 20121027_181338The actual date that we want is the fourth field in the output.
-
We pass the
exiftooloutput toawkso that it can extract the field that we want:awk '{print $4".jpg"}'This selects the date field and also adds on the
.jpgextension.