#!/bin/bash

# Date: 2020-05-10T15:27Z
# Author: Steven Baltakatei Sandoval
# License: GPLv3+
# Ref./Attrib:
#  [1]: John1024 (2014-08-06). ["linux-shell: renaming files to creation time"](https://stackoverflow.com/a/25153352). Licensed CC BY-SA 4.0.
#  [2]: Computer Hope (2019-05-04). ["Bash read builtin command"](https://www.computerhope.com/unix/bash/read.htm).

# Description: Preappends modification time to all files in current
# working directory.

# Usage: bkdatename

# Prompt to change filenames for all files in working directory.
read -p "Append each file's modification date & time to the start of its name in the current working directory? (y/n)" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]] # see [2]
then
    #Perform renaming operation.
    for f in *
    do
	FILE_NAME="$f"
	FILE_MDATE="$(date -r "$f" +"%Y%m%dT%H%M%S")" # See [1].
	FILE_NAME_NEW="$FILE_MDATE".."$FILE_NAME"
	# Display new file name
	echo "Renaming $f to:""$FILE_NAME_NEW"
	cp -p ./"$f" ./"$FILE_NAME_NEW"
	rm ./"$f"
    done
    echo "Done."
fi