Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

remove file extension

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-19-2007
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 14 April 2016, 8:32 PM EDT
Posts: 216
Thanks: 10
Thanked 7 Times in 7 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 Unix and Linux 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 Unix and Linux 07-19-2007
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 8 February 2016, 5:12 PM EST
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 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 Unix and Linux 07-19-2007
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 8 February 2016, 5:12 PM EST
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 13 Times in 13 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 Unix and Linux 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 Linux
Sponsored Links
    #6  
Old Unix and Linux 07-19-2007
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 14 April 2016, 8:32 PM EDT
Posts: 216
Thanks: 10
Thanked 7 Times in 7 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 Unix and Linux 07-19-2007
prvnrk prvnrk is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 14 April 2016, 8:32 PM EDT
Posts: 216
Thanks: 10
Thanked 7 Times in 7 Posts
Thanks vino, i got it done.

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

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:00 AM
About the file extension kunalpatil09 UNIX for Dummies Questions & Answers 3 05-21-2010 05:14 PM
.bz2 extension file pritish.sas Shell Programming and Scripting 1 07-21-2009 05:21 AM
how can i remove files with extension in many folders ateya UNIX for Dummies Questions & Answers 6 12-15-2008 10:05 AM



All times are GMT -4. The time now is 05:13 PM.