~arivigo/phrenamer

ref: 19480fc7c1685e8647a07afeceaea2bb8c7bda54 phrenamer/phrenamer -rwxr-xr-x 691 bytes
19480fc7Ariadna Vigo LICENSE: updated my email address 9 months 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
#!/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)
! [ "$files" ] && die 'Error: no files to rename.'

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

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