Computing.Net > Forums > Unix > Rename file extension in script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Rename file extension in script

Reply to Message Icon

Name: hismail
Date: May 19, 2008 at 00:33:29 Pacific
OS: Linux Redhat
CPU/Ram: 4gb/2/gb
Product: HP
Comment:

Hi,
I would like to rename files in a directory.
for eg:
ls -ltr *.bup.*
would give a result of:
ma.bup.Wednesday
hw.bup.Wednesday
etc
I need to trim the last field i.e "Wednesday"
to just ma.bup automatically everyday


Thanks
Hamim



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: May 19, 2008 at 13:33:39 Pacific
Reply:

One way is to use the ksh/bash pattern matching operators. Change the cp to mv once you get the script working the way you want:


#!/bin/ksh

f=ma.bup.wed

v=${f%.*}

cp $f $v


0

Response Number 2
Name: hismail
Date: May 19, 2008 at 23:43:18 Pacific
Reply:

Hi Nails
I tried this:
for f in *.Friday
do
mv $f ${f%*.}
done
but all it does it is puts an extra .bup after my original file??
I need to trim the file to just have
"ma.bup" & not ma.bup.Friday.bup

thanks
Hamim


0

Response Number 3
Name: nails
Date: May 20, 2008 at 08:37:15 Pacific
Reply:

Your syntax is incorrect:

It's -> ${f%.*}

NOT -> ${f%*.}


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Rename file extension in script

multiple renaming files & extension www.computing.net/answers/unix/multiple-renaming-files-amp-extension/6314.html

Utility or script for renaming files on www.computing.net/answers/unix/utility-or-script-for-renaming-files-on-/2538.html

Renaming files with awk www.computing.net/answers/unix/renaming-files-with-awk/8036.html