]> git.scottworley.com Git - picsort/blobdiff - rename-to-date
Include referenced rename-to-date script
[picsort] / rename-to-date
diff --git a/rename-to-date b/rename-to-date
new file mode 100755 (executable)
index 0000000..0230e48
--- /dev/null
@@ -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
+