Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

shell script to rename files? (mv *.txt --> *_xx.txt)

0 views
Skip to first unread message

mags doheny

unread,
May 31, 2000, 3:00:00 AM5/31/00
to
I realise that it's easy, for example, to copy/move files to the
original filename with a new extension in /bin/sh, but I need a shell
script that will rename the part preceding the file extension,
i.e. mv *.txt --> *_xx.txt
Do i need to strip the file extension first using sed maybe? (if so,
how?)
Alternatively, would mmv work - is there a Solaris version?

Thanks a mil,
Mags.

mdoheny.vcf

Paul Brandariz x6546

unread,
May 31, 2000, 3:00:00 AM5/31/00
to

Try:

for I in *.txt
do
TARGET=`echo $I | sed 's/\.txt$/_xx.txt$/'`
mv $I $TARGET
done

--
___________________________________________________________________________
Paul R. Brandariz E-mail Internet: bran...@lore.kla-tencor.com
KLA-Tencor Corporation
One Technology Dr.
Milpitas, CA 95035

Mike Purdie

unread,
Jun 1, 2000, 3:00:00 AM6/1/00
to
Have a look at basename(1)

Paul Brandariz x6546 <bran...@lore.kla-tencor.com> wrote in message
news:3935348E...@lore.kla-tencor.com...

Jehsom

unread,
Jun 1, 2000, 3:00:00 AM6/1/00
to
In comp.unix.shell Mike Purdie <mike....@eds.com> wrote:
>> for I in *.txt
>> do
>> TARGET=`echo $I | sed 's/\.txt$/_xx.txt$/'`
>> mv $I $TARGET
>> done

> Have a look at basename(1)

basename does not help the original poster. Reread the post, and
then read the manpage for basename.

Moshe

--
jehsom@ (angband.org, bellsouth.net, burdell.org, cc.gatech.edu,
nullity.dhs.org, polter.net, resnet.gatech.edu, treklink.net, usa.net,
wreck.org, yo.dhs.org); (gte741e, mj116) @prism.gatech.edu; ICQ 1900670

Walter T Rejuney

unread,
Jun 1, 2000, 3:00:00 AM6/1/00
to
Paul Brandariz x6546 wrote:
>
> mags doheny wrote:
> >
> > I realise that it's easy, for example, to copy/move files to the
> > original filename with a new extension in /bin/sh, but I need a shell
> > script that will rename the part preceding the file extension,
> > i.e. mv *.txt --> *_xx.txt
> > Do i need to strip the file extension first using sed maybe? (if so,
> > how?)
> > Alternatively, would mmv work - is there a Solaris version?
> >
> > Thanks a mil,
> > Mags.
>
> Try:
>
> for I in *.txt
> do
> TARGET=`echo $I | sed 's/\.txt$/_xx.txt$/'`
> mv $I $TARGET
> done

Messy, messy, messy.

Better:

#!/bin/ksh
IFS='';
typeset -r Target="$1";
if [[ ${#Target} -eq 0 ]];then
print -u2 "No file specified";
exit 1;
fi
Path="${Target%/*}";
FileName="${Target##*/}";
Name="${FileName%%.*}";
Ext="${FileName##*.}";
Newname="_xx";
typeset Oname="${Newname}.${Ext}";
mv "${Target}" "${Oname}";
if [[ $? -ne 0 ]];then
print -u2 "operation failed";
exit 1;
fi
exit 0;

Manfred Bartz

unread,
Jun 1, 2000, 3:00:00 AM6/1/00
to
mags doheny <mdo...@netscape.com> writes:

> I realise that it's easy, for example, to copy/move files to the
> original filename with a new extension in /bin/sh, but I need a shell
> script that will rename the part preceding the file extension,
> i.e. mv *.txt --> *_xx.txt
> Do i need to strip the file extension first using sed maybe? (if so,
> how?)

With bash or ksh you can use the built-in pattern manipulation
capability.

Assume n="FileName.txt", then ${n%.txt} will expand to "FileName"
and you could do something like this:

$ for n in *.txt; do mv $n ${n%.txt}_xx.txt; done

> Alternatively, would mmv work - is there a Solaris version?

Yes, mmv would work: mmv '*.txt' '#1_xx.txt'

If you can't get it precompiled from Sun, you
could just grab the source from some archive and compile it yourself.

<http://www.faqs.org/faqs/unix-faq/faq/part2/>

Also have a look at man basename

HTH
--
Manfred Bartz

0 new messages