diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2020-08-12 00:47:37 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2020-08-12 00:47:37 +0200 |
commit | a911bcfd5dc7827b597749b47d69649f88653476 (patch) | |
tree | 93545a17eb9321de9d9f184e0fa416a3772a4f63 /scripts | |
parent | 4df28c17b5c0d96e22ec5ff90a05af5fe1549fb6 (diff) | |
download | dotfiles-a911bcfd5dc7827b597749b47d69649f88653476.tar.gz |
bulkrename.sh
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/bulkrename.sh | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/scripts/bulkrename.sh b/scripts/bulkrename.sh new file mode 100755 index 0000000..9df17d2 --- /dev/null +++ b/scripts/bulkrename.sh @@ -0,0 +1,41 @@ +#!/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} + +while [ -z "$TMPFILE" -o -e "$TMPFILE" ]; do + TMPFILE="/tmp/bulkrename.$RANDOM" +done +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 Error. does target already exist? + fi + fi + + i=$(( i + 1 )) +done < "$TMPFILE" + +rm "$TMPFILE" |