Search:  

 
 
   All ForumsHot TopicsGallery






how-to block ads


 
Forums » Tech and Talk » OS and Software » All Things Unix » quick way to remove file extension?
Search Topic:
Uniqs:
1460
Share Topic:
RSS topic:
toggle:
flat / full
normal / watch
Posting:
Post a:
Post a:
Who am I »
« OpenSolaris boot issue  
AuthorAll Replies


laura
Domestic Bliss
Premium
join:2002-04-16
San Jose, CA
quick way to remove file extension?

I am a scripting newbie... or maybe this can be done on one line but all I need is to remove the .sav extension off of all the files in the directory...
--
43 things


Happyrat
Google Is Your Best Friend
Premium
join:2002-07-01
Disneyland
info rename


laura
Domestic Bliss
Premium
join:2002-04-16
San Jose, CA
reply to laura
my bad this is on Solaris and rename doesn't seem to exist


nwrickert
sand groper
Premium,MVM
join:2004-09-04
Geneva, IL
·AT&T U-Verse
·AT&T Midwest

reply to laura
It isn't clear whether you want to rename the files, or you want to make available to your script the name without extension. If you are scripting a rename, you need both.

The "basename" command will give you the file name without extension.

For renaming, try something like

for file in *.sav
do
mv $file `basename $file .sav`
done


--
AT&T dsl; Westell 2200 modem/router; SuSE 10.1; firefox 2.0.0.5


laura
Domestic Bliss
Premium
join:2002-04-16
San Jose, CA
reply to laura
I just wanted to rename the files.. that script works awesome. Thank you!
--
43 things


laura
Domestic Bliss
Premium
join:2002-04-16
San Jose, CA

reply to laura
New question... if I wanted the user to provide a file extension as an argument and I process it within the script.. how can I check that at least one file exists?

if [ -e *$@ ] does not work
--
43 things


nwrickert
sand groper
Premium,MVM
join:2004-09-04
Geneva, IL
·AT&T U-Verse
·AT&T Midwest

I would maybe try:

for file in "*.$1"
do
if [ "$file" = "*.$1" ] ; then
echo "No files match *.$1"
exit 1
fi
mv $file `basename $file ".$1"`
done

That probably won't work if there is white space in "$1".

It depends on the fact that sh/bash will evaluate "*" as just "*" when there are no matching files.

--
AT&T dsl; Westell 2200 modem/router; SuSE 10.1; firefox 2.0.0.5


jdong
Eat A Beaver, Save A Tree.
Premium
join:2002-07-09
Rochester, MI
clubs:

reply to nwrickert
said by nwrickert See Profile :

It isn't clear whether you want to rename the files, or you want to make available to your script the name without extension. If you are scripting a rename, you need both.

The "basename" command will give you the file name without extension.

For renaming, try something like

for file in *.sav
do
mv $file `basename $file .sav`
done


^^ that would be my prefered name, but I'd wrap things in a few more levels of quotes, such as


for file in "*.sav*; do
mv "$file" "`basename "$file" .sav`"
done


Otherwise, expect borkage with filenames with spaces.

--
UbuntuForums Administrator: try Ubuntu Linux


unixninja

@gd-ais.com
rename .ext " " *.ext
Forums » Tech and Talk » OS and Software » All Things UnixWho am I »
« OpenSolaris boot issue  


Monday, 23-Nov 06:41:13 Terms of Use | Privacy Policy | Hosting by www.nac.net - DSL,Hosting & Co-lo | feedback | contact
over 10 years online! © 1999-2009 dslreports.com.
page compression OFF
Most commented news this week
· [229] Weekend Open Thread
· [117] Verizon Again Hints At Metered Billing
· [98] There's Still No Evidence That Metered Billing Is Necessary
· [97] Will AOL's Implosion Ever End?
· [85] Spain Declares Broadband A Legal Right
· [75] Deploying FTTH Without Digging Things Up
· [74] Verizon To Be Tested By Unofficial Droid Tethering
· [74] Femtocells Are A No Show
· [67] Verizon To AT&T: The Truth Hurts
· [60] Chicago Tribune Visits 'Comcast University'
Most people now reading
· Extra charge to use Master Card instead of Visa? [General Questions]
· TekSavvy Price Increase? [TekSavvy]
· Connecting to Google Voice Via SIP [VOIP Tech Chat]
· Windows 7 boot manager editing questions [Microsoft Help]
· DIR-655 New Beta 1.32b09 [D-Link]
· Facebook Cures Depression [Canadian Chat]
· [How to] Install Asterisk on an Asus WL-520GU router [VOIP Tech Chat]
· [WIN7] Outlook express under Windows 7? [Microsoft Help]
· MLPPP and MikroTik [TekSavvy]
· Slow speeds in the evenings [TekSavvy]