File Renaming Using an Editor
Here is a script that builds on the principle of using a text editor to rename files. This is sometimes useful, since editors have more advanced features available, like block mode editing, compared to a shell.
The script generates another script that performs the renaming, but before the script is called, it's loaded into the editor. It operates on the current directory or another one if it's specified as an argument.
#!/bin/bash SCRIPT="_rename.bash" if [ ! -z "$1" ]; then cd "$1" fi if [ -e "./$SCRIPT" ]; then echo "$SCRIPT already exists, delete it if necessary." exit 1; fi LONGEST=0 for FILE in *; do if [ ! -e "$FILE" ]; then echo "Error: Directory empty!" exit 1; fi FILE="${FILE//\\/\\\\}" FILE="${FILE//\'/\'}" if [ ${#FILE} -gt $LONGEST ]; then LONGEST=${#FILE} fi done echo "#!/bin/bash" > "./$SCRIPT" for FILE in *; do if [ "$FILE" = "$SCRIPT" ]; then continue fi FILE="${FILE//\\/\\\\}" FILE="${FILE//\'/\'}" echo -n "mv -v $'$FILE' " >> "./$SCRIPT" PAD=$(( $LONGEST - ${#FILE} )) while [ $PAD -gt 0 ]; do echo -n " " >> "./$SCRIPT" PAD=$(( $PAD - 1 )) done echo "$'$FILE'" >> "./$SCRIPT" done chmod +x "./$SCRIPT" if [ "${EDITOR##*/}" = "vim" ]; then $EDITOR -c "set nowrap" "./$SCRIPT" else $EDITOR "./$SCRIPT" fi bash "./$SCRIPT" rm -f "./$SCRIPT"