remove file extension | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

remove file extension

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-19-2007
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 27 August 2014, 4:54 PM EDT
Posts: 207
Thanks: 7
Thanked 5 Times in 5 Posts
remove file extension

Hi ALL,

I'm new to this forum.

Thanks and congrats to all for their great efforts building this site simply superb for all unix administrators.

My requirement is to remove extensions of the files in the current directory. I'm doing it using below script which is working but i think it is very inefficient as i use lot of variables.I want to get done by avoiding usage of tempfile file and any other temp files being used.


#!/usr/bin/bash
ls -l |awk '{print $9}' >/list
cat /list |while read line
do
echo $line >/tempfile
first=`cut -f1 -d'.' /tempfile`
echo $first >>/output.txt
mv $line $first
done


Thanks
Praveen RK
Sponsored Links
    #2  
Old 07-19-2007
lorcan lorcan is offline
Registered User
 
Join Date: May 2007
Last Activity: 13 August 2012, 12:14 AM EDT
Posts: 224
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
ls -1 | sed 's/\(.*\)\..*/\1/'

Sponsored Links
    #3  
Old 07-19-2007
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Perhaps this.


Code:
for file in *
do
  if [ -f $file ] ; then
    # name without extension
    name=${file%\.*}
    echo ${name} >> output.txt
  fi ;
done

    #4  
Old 07-19-2007
vino's Avatar
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 9 April 2013, 8:19 AM EDT
Location: Sydney, Down Under
Posts: 2,847
Thanks: 0
Thanked 12 Times in 12 Posts
Quote:
Originally Posted by lorcan View Post
Code:
ls -1 | sed 's/\(.*\)\..*/\1/'

You really dont need the -1 if you are going to pipe the output to some other command.
Sponsored Links
    #5  
Old 07-19-2007
lorcan lorcan is offline
Registered User
 
Join Date: May 2007
Last Activity: 13 August 2012, 12:14 AM EDT
Posts: 224
Thanks: 0
Thanked 2 Times in 2 Posts
Quote:
Originally Posted by vino View Post
You really dont need the -1 if you are going to pipe the output to some other command.
Great, Thanks for letting me know
Sponsored Links
    #6  
Old 07-19-2007
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 27 August 2014, 4:54 PM EDT
Posts: 207
Thanks: 7
Thanked 5 Times in 5 Posts
Thanks Lorcan for your quick reply. The command just displays the files without extension but how to add "mv" most efficiently as i need to rename the files?

Praveen RK
Sponsored Links
    #7  
Old 07-19-2007
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 27 August 2014, 4:54 PM EDT
Posts: 207
Thanks: 7
Thanked 5 Times in 5 Posts
Thanks vino, i got it done.

Lorcan, thanks and ignore my last msg.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
creating separate directories according to file extension and keeping file in different directory as Deekay.p UNIX for Dummies Questions & Answers 2 07-28-2010 10:00 AM
About the file extension kunalpatil09 UNIX for Dummies Questions & Answers 3 05-21-2010 04:14 PM
.bz2 extension file pritish.sas Shell Programming and Scripting 1 07-21-2009 04:21 AM
how can i remove files with extension in many folders ateya UNIX for Dummies Questions & Answers 6 12-15-2008 09:05 AM
How to get file extension shirleyeow Shell Programming and Scripting 17 01-17-2008 07:40 AM



All times are GMT -4. The time now is 06:00 AM.