Skip to main content
2 of 7
changed request from "creation date" to "change date"
jshlke
  • 177
  • 1
  • 6

Add Creation Date and Variable to Batch Rename

I wish to batch-rename a bunch of files to this format

<change-date>_<shootname>_<number auto-incremented by creation date>.NEF

From here I have a perfect 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 

What I am looking for now is to add the file-change-date and the shootname at the beginning. Hard coding it works well, but it would be great if it could be automated. In the the file name should look like this

20140707_NameOfShoot_0001.NEF

For the timestamp I find articles on strftime(), mktime(), systime() but I don't understand how to use them to return file change 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.

jshlke
  • 177
  • 1
  • 6