X-Git-Url: http://git.scottworley.com/picsort/blobdiff_plain/a05e21670fd3fd2d8e0de963a21ec871b6725f52..70f277664128fdf614dd61f3b8737829ba28638e:/rename-to-date?ds=inline diff --git a/rename-to-date b/rename-to-date new file mode 100755 index 0000000..0230e48 --- /dev/null +++ b/rename-to-date @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +set -euo pipefail +mv_command=mv +skip_duplicates=true +while getopts l opt;do + case $opt in + l) mv_command=ln;; + esac +done +shift "$((OPTIND - 1))" + +for f; do + if [[ ! -e "$f" ]];then + echo "Input file \"$f\" does not exist." >&2 + continue + fi + extension=${f##*.} + new_name="$(find "$f" -printf "%TY-%Tm-%Td %TT %TZ").$extension" + while [[ -e "$new_name" ]];do + if $skip_duplicates;then + if [[ "$(find "$f" -printf '%i')" == "$(find "$new_name" -printf '%i')" ]];then + echo "Skipping $f: it is already $new_name (same inode)" + fi + if cmp "$f" "$new_name";then + echo "Skipping $f: it is already $new_name" + continue 2 + fi + fi + fields=( $new_name ) + t=${fields[1]#*.} + if (( ${#t} != 10 ));then + echo "Expected $t (from $new_name) to be 10 characters" >&2 + exit 1 + fi + t=$(printf "%010d" $(( $(sed 's/^0*//' <<< "$t") + 1 )) ) + new_name="${fields[0]} ${fields[1]%.*}.$t ${fields[2]}" + done + $mv_command -vi "$f" "$new_name" +done +