: # rename - rename files ~ ala VMS. # # Usage: rename p1 [p2] # rename - p2 # rename p1 - # # Let p1 and p2 be simple patterns. # # If only p1 is present, it is removed. # If both are present, p2 replaces p1. # If p1 is a dash, p2 is used as a suffix. # If p2 is a dash, p1 is used as a prefix. # If both are dashes, you get -*-. # Slashes are disallowed, for safety... # # if p1 is -c, a copy is done instead, # and args p2 and p3 become p1 and p2. # # Written by Rich Morin and Vicki Brown #set -x # if [ -n "`echo "$*" | sed -n '/\//p'`" ]; then echo "No slashes, please..."; exit 1 fi cmd=mv text=Rename if [ "$1" = "-c" ]; then cmd=cp text=Copy shift fi if [ "$1" = "-" -o "$2" = "-" ]; then if [ "$1" = "-" ]; then S="$2"; fi if [ "$2" = "-" ]; then P="$1"; fi for old in *; do echo -n "$text '$old' to '$P$old$S'? (y/n): " read answer case $answer in [Yy]) $cmd -i $old $P$old$S ;; *) ;; esac done exit 0 fi # Escape 1st arg. in case there is magic afoot. # (But don't escape numbers, to avoid magic...) case $1 in [0-9]*) getnew='new=`echo $old | sed "s/$1/$2/"`';; *) getnew='new=`echo $old | sed "s/\$1/$2/"`';; esac for old in *$1*; do eval $getnew echo -n "$text '$old' to '$new'? (y/n):" read answer case $answer in [Yy]) $cmd -i $old $new ;; *) ;; esac done