]> git.scottworley.com Git - picsort/blob - rename-to-date
0230e48bb86874bc19afcece4b9e6d587256850d
[picsort] / rename-to-date
1 #!/usr/bin/env bash
2 set -euo pipefail
3 mv_command=mv
4 skip_duplicates=true
5 while getopts l opt;do
6 case $opt in
7 l) mv_command=ln;;
8 esac
9 done
10 shift "$((OPTIND - 1))"
11
12 for 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"
39 done
40