scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

virename (612B)


      1 #!/bin/sh
      2 
      3 # Rename all files in the current directory with a visual editor.
      4 # Similar to sel add all && sel mv, but it actually works.
      5 
      6 file=$(mktemp)
      7 file2=$(mktemp)
      8 editor=${EDITOR:-vi}
      9 
     10 ls | tee "$file" > "$file2"
     11 $editor "$file2"
     12 
     13 if [ "$(wc -l "$file"  | awk '{print $1}')" != \
     14      "$(wc -l "$file2" | awk '{print $1}')" ]; then
     15 	echo "Error reading new file names"
     16 else
     17 	paste "$file" "$file2" | while read -r f; do
     18 		fold=$(echo "$f" | sed 's/	.*//')
     19 		fnew=$(echo "$f" | sed 's/.*	//')
     20 		if [ "$fold" != "$fnew" ]; then
     21 			mv "$fold" "$fnew" \
     22 			|| echo "Error renaming $fold to $fnew"
     23 		fi
     24 	done
     25 fi