site Search:


 
    All Forums Hot Topics Gallery






how-to block ads


 
Search Topic:
Uniqs:
4964
Share Topic
Posting?
Post a:
Post a:
Links: ·Forum FAQ ·Attitude Adjustment ·Linux docs ·DistroWatch ·OPLM ·FreeBSD Handbook
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


Happyrat6
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
kudos:7
Reviews:
·AT&T U-Verse

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
kudos:7
Reviews:
·AT&T U-Verse

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
kudos:1

reply to nwrickert

said by nwrickert:

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


Tuesday, 29-May 03:58:36 Terms of Use & Privacy | feedback | contact | Hosting by nac.net - DSL,Hosting & Co-lo
over 12.5 years online © 1999-2012 dslreports.com.
Most commented news this week
Hot Topics