How to do mv *.xyz *.abc in shell script??

Greg Hunt hunt at dg-rtp.dg.com
Sat Sep 8 04:15:13 AEST 1990


In article <5569 at minyos.xx.rmit.oz>, rcoahk at chudich.co.rmit.oz (Alvaro Hui Kau) writes:
> Hi all experts out here!
> 
> Sorry for the stupid query, but I just don't know how to do
> it. Once in a while I want to change the names of a bunch of
> files from *.xyz to *.abc for example. I know there is a better
> way to do this than doing it one by one but I just can't
> find any way to do it. I know it can be done using shell script
> but I don't know (yet) how to write them.
> 
> So, Is there anyone out there can help me on this problem?
> 
> Thanks in advance.
>

There are no stupid questions.  Just ones you don't know the answers
to yet.  This is how I change the names of files like you need to in
the Bourne shell:

    for tmp in $* ; do
        mv $tmp `basename $tmp .xyz`.abc
    done

The basename program returns just the file name portion of a pathname,
and optionally removes a suffix.  Since there is no pathname portion
if you're in the directory the files exist in, all that this example
does is to remove the suffix.  Then it puts on the new suffix, and
the files get renamed the way you want.

Put the above lines into a file named whatever you want with an editor.
Exit the editor.  Then type 'chmod 755 your_script_name'.  This changes
the file permissions to user{read,write,execute}, group{read,execute},
other{read,execute}.  The "execute" permission is the key to being able
to run shell scripts.  To use the script, type:

    your_script_name *.xyz

As you learn more about scripts, you can make it more general by
allowing you to specify the old and new suffixes as arguments to the
script as well (hint - look at the shift shell command and specify the
old and new suffixes as the first two arguments).

Enjoy!

--
Greg Hunt                        Internet: hunt at dg-rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC       These opinions are mine, not DG's.



More information about the Comp.unix.shell mailing list