| 1 | #!/bin/bash |
| 2 | |
| 3 | # Date: 2020-05-10T15:27Z |
| 4 | # Author: Steven Baltakatei Sandoval |
| 5 | # License: GPLv3+ |
| 6 | # Ref./Attrib: |
| 7 | # [1]: John1024 (2014-08-06). ["linux-shell: renaming files to creation time"](https://stackoverflow.com/a/25153352). Licensed CC BY-SA 4.0. |
| 8 | # [2]: Computer Hope (2019-05-04). ["Bash read builtin command"](https://www.computerhope.com/unix/bash/read.htm). |
| 9 | |
| 10 | # Description: Preappends modification time to all files in current |
| 11 | # working directory. |
| 12 | |
| 13 | # Usage: bkdatename |
| 14 | |
| 15 | # Prompt to change filenames for all files in working directory. |
| 16 | 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 |
| 17 | echo |
| 18 | if [[ $REPLY =~ ^[Yy]$ ]] # see [2] |
| 19 | then |
| 20 | #Perform renaming operation. |
| 21 | for f in * |
| 22 | do |
| 23 | FILE_NAME="$f" |
| 24 | FILE_MDATE="$(date -r "$f" +"%Y%m%dT%H%M%S")" # See [1]. |
| 25 | FILE_NAME_NEW="$FILE_MDATE".."$FILE_NAME" |
| 26 | # Display new file name |
| 27 | echo "Renaming $f to:""$FILE_NAME_NEW" |
| 28 | cp -p ./"$f" ./"$FILE_NAME_NEW" |
| 29 | rm ./"$f" |
| 30 | done |
| 31 | echo "Done." |
| 32 | fi |