*BSD News Article 42554


Return to BSD News archive

Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!bunyip.cc.uq.oz.au!munnari.oz.au!constellation!bubba.ucc.okstate.edu!news.ksu.ksu.edu!vixen.cso.uiuc.edu!howland.reston.ans.net!news.sprintlink.net!uunet!newsfeed.pitt.edu!godot.cc.duq.edu!smirnoff.ittc.pgh.wec.com!news.galt.com!alex
From: alex@phred.org (alex wetmore)
Newsgroups: comp.unix.bsd
Subject: Re: Newbie Question: mv *.t *.txt ????
Date: 16 Feb 1995 04:05:06 GMT
Organization: Phred Networking
Lines: 34
Message-ID: <3huiti$4lr@dagny.galt.com>
References: <D41nyo.Mon@ritz.mordor.com>
NNTP-Posting-Host: phred.ws.cc.cmu.edu
X-Newsreader: TIN [version 1.2 PL2]

Hany Nagib (hany@ritz.mordor.com) wrote:
: I know this must be a very simple question, but it's not in the FAQ.
: "mv" looks like it works exactly like "rename" in DOS, except when using 
: wild card. For example if I wanted to rename all my .t files to .txt, I 
: expect "mv *.t *.txt" to work .. but it doesn't. Why ? And how do I 
: accomplish this in BSD unix ?

DOS leaves globbing (converting wildcards into filenames) up to application
programs, while Unix does it in the shell.  So the "mv" program sees
the command line "mv a.t b.t c.t" (if your directory has the files a.t,
b.t, and c.t, but no .txt files), and doesn't know how to handle that.  

Try this: (in sh)

for oldf in *.t
do
   newf=`echo $oldf | sed 's/.t$/.txt/g'`
   mv $oldf $newf
done

I'm sure there are shorter ways to do it, but the above works for
me.  You could turn it into the shell script:

#!/bin/sh
for oldf in *.$1
do
   newf=`echo $oldf | sed "s/.$1\$/.$2/g"`
   mv $oldf $newf
done

which takes the old extension as argument one, the new extension
as argument two, and renames everything.  

alex