The question is completely rewritten due to things learned from the first two answers and comments that I was unaware of when first asking.
After a photo shoot I come home with files that look like _DSC1234.NEF. NEF is Nikon's camera raw file format, so EXIF data is present in the files.
I would like to automatically rename them with three parts:
- Creation date in the format
YYYYMMDD - Name of Shoot
- Number of Image
So the final file name should look like this
20140707_NameOfShoot_0001.NEF
There are a few issues:
ad 1. Creation date
sometimes I can only copy and rename the files a few days after shooting, so the date should reflect the date the picture was taken, not the date it was copied. mtime seems like the best bet here or if possible creation date from EXIF.
ad 2: Name of Shoot This would ideally be a variable I could set as a parameter on calling the script.
ad 3. Number of Image
This should reflect the age of the image, the oldest having the lowest number. The problem is that cameras usually restart numbering at 0000 after they hit 9999. So 9995-9999 can potentially be older than 0000-0004. I am looking for a solution that reflects file age and in this special case would rename
- _DSC0000.NEF -> 20140707_FOO_0004.NEF
- _DSC0001.NEF -> 20140707_FOO_0005.NEF
- _DSC0002.NEF -> 20140707_FOO_0006.NEF
- ...
- _DSC9997.NEF -> 20140707_FOO_0001.NEF
- _DSC9998.NEF -> 20140707_FOO_0002.NEF
- _DSC9999.NEF -> 20140707_FOO_0003.NEF
Again, either mtime or if possible creation date from EXIF seem right.
From herehere I have a working solution which renames all .NEF-files in a folder by date:
find -name '*.NEF' |
gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.NEF\n", $0, a++ }' |
bash
Hard coding file-modification-date and the shootname works well, but it would be great if it could be automated.
For the timestamp I find articles on strftime(), mktime(), systime() but I don't understand how to use them to return file modification date. I also tried to add DATE=$(date +"%Y%m%d") and add $DATE to the gawk-line, which leads to deletion of all files in current folder (and probably is systime anyway and not change time).
For the variable, I tried
gawk 'BEGIN{ a=1 }{ printf "mv %s $1_%04d.NEF\n", $0, a++ }' |
and call the script with ./rename FOO, but FOO is ignored in renaming.