aboutsummaryrefslogtreecommitdiff
path: root/scripts/bulkrename.sh
blob: 186326aecc92d19e50c29da2b6dd8e0cde011c52 (plain)
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
#!/bin/bash
# Rename multiple files at once using a text editor
# File list can be given as args
# if no args, all files in pwd are used

EDITOR=${EDITOR=:vi}

TMPFILE="$(mktemp)"
touch "$TMPFILE"

FILES=( )
if [ $# -eq 0 ]; then
	FILES=( * )
else
	FILES=( $@ )
fi

# List current dir
for f in "${FILES[@]}"; do
	echo $f >> "$TMPFILE"
done

eval "$EDITOR $TMPFILE"

i=0
while read line; do
	if [ "$line" != "${FILES[$i]}" -a -e "${FILES[$i]}" -a ! -e "$line" ]; then
		echo "${FILES[$i]} -> $line"
		if [ -e "${FILES[$i]}" -a ! -e "$line" ]; then
			mv "${FILES[$i]}" "$line"
		else
			echo "Skipping ${FILES[$i]} -> $line . Target already exists."
		fi
	fi

	i=$(( i + 1 ))
done < "$TMPFILE"

rm "$TMPFILE"