Bash – Append human readable timestamp of last edit to filename

bashfilestimestamps

I would like to add the time of the last change to the file to its filename using the bash.

For example this output of ls -la:

 -rw-rw-r-- 1 beginner beginner 5382 Dec  1 17:18 B_F1-1.xml

should become

 -rw-rw-r-- 1 beginner beginner 5382 Dec  1 17:18 B_F1-1_20141201T1718.xml

How could I do this to all files in .?

Best Answer

You can try something like:

EXT=${FILE#*.}
NAME=${FILE%%.*}

mv "$FILE" "$NAME$(date --reference "$FILE" '+%Y%m%dT%H%M').$EXT"

in a script, if your date supports --reference, which picks up the last modification date of the reference file.

Related Question