[comp.unix.shell] moving *.x to *.y

rhartman@thestepchild.sgi.com (Robert Hartman) (06/25/91)

>In article <1991Jun24.144854.8879@ap542.uucp> mike@ap542.uucp (Mike Hoffmann) writes:
>}
>}Hi!
>}
>}I have no idea if this is a FAQ, but anyway.
>}
>}I'm looking for a wildcard expansion mechanism, where for example I could
>}do mv *.c *.o.
>}E.g. an expansion where I only get the * part. 

OK.  Enough.  Here's a Bourne shell script to do this.  You have to
specify the old pattern (.c), and the new pattern (.o), followed by the
list of files to move (*.c).  The four backslashes in the sed command
are necessary to protect against incorrect matches for "." in a suffix.
This script isn't restricted to changing suffixes.

-r

    #! /bin/sh
    #
    #  gm: group move -- change pattern in list of filenames
    #
    
    case $# in
    0)	echo Usage: gm oldpat newpat filename ... ; exit ;;
    esac
    
    oldpat=$1
    newpat=$2
    shift ; shift
    
    while test $# -gt 0 ; do
        oldname=$1
        echo $oldname '\c'
        newname=`echo $oldname | sed "s/\\\\$oldpat/$newpat/"`
        case $newname in
        $oldname) echo "not moved" ;;
        *)  mv $oldname $newname && echo "-> $newname"
        esac
        shift
    done