]> git.scottworley.com Git - picsort/blame - rename-to-date
Include referenced rename-to-date script
[picsort] / rename-to-date
CommitLineData
70f27766
SW
1#!/usr/bin/env bash
2set -euo pipefail
3mv_command=mv
4skip_duplicates=true
5while getopts l opt;do
6 case $opt in
7 l) mv_command=ln;;
8 esac
9done
10shift "$((OPTIND - 1))"
11
12for f; do
13 if [[ ! -e "$f" ]];then
14 echo "Input file \"$f\" does not exist." >&2
15 continue
16 fi
17 extension=${f##*.}
18 new_name="$(find "$f" -printf "%TY-%Tm-%Td %TT %TZ").$extension"
19 while [[ -e "$new_name" ]];do
20 if $skip_duplicates;then
21 if [[ "$(find "$f" -printf '%i')" == "$(find "$new_name" -printf '%i')" ]];then
22 echo "Skipping $f: it is already $new_name (same inode)"
23 fi
24 if cmp "$f" "$new_name";then
25 echo "Skipping $f: it is already $new_name"
26 continue 2
27 fi
28 fi
29 fields=( $new_name )
30 t=${fields[1]#*.}
31 if (( ${#t} != 10 ));then
32 echo "Expected $t (from $new_name) to be 10 characters" >&2
33 exit 1
34 fi
35 t=$(printf "%010d" $(( $(sed 's/^0*//' <<< "$t") + 1 )) )
36 new_name="${fields[0]} ${fields[1]%.*}.$t ${fields[2]}"
37 done
38 $mv_command -vi "$f" "$new_name"
39done
40