~arivigo/phrenamer

phrenamer/phrenamer -rwxr-xr-x 756 bytes
3edadce7Ariadna Vigo LICENSE: adding 2022 5 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh
# See LICENSE file for copyright and license details.

die() {
	echo "$1" >&2
	exit 1
}

usage() {
	die 'usage: phrenamer [-d] [dir]'
}

while getopts :d opt; do
	case "$opt" in
	d)	dry='t' ;;
	?)	usage ;;
	esac
done

shift $((OPTIND - 1))
dir="${1:-$(pwd)}"

files=$(ls "${dir}/"*.jpg "${dir}"/*.jpeg "${dir}/"*.JPG 2> /dev/null)
if [ -z "$files" ]; then
	die 'Error: no files to rename.'
fi

filenum=$(echo "$files" | wc -l)
zeroes=$((${#filenum} - 1)) # minus '\0'

IFS="
"
count=0
for fname in $files; do
	count=$((count + 1))
	dirpart=$(dirname "$fname")
	newname=$(printf "%s/%0${zeroes}d.jpg" "$dirpart" "$count")
	
	if ! [ -f "$newname" ]; then
		echo "$fname -> $newname"
		if [ -z "$dry" ]; then
			mv "$fname" "$newname"
		fi
	fi
done