Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-19-2007
Registered User
 

Join Date: Jul 2007
Posts: 145
Thanks: 0
Thanked 0 Times in 0 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
Registered User
 

Join Date: May 2007
Posts: 220
Thanks: 0
Thanked 0 Times in 0 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
Location: Bangalore, India
Posts: 2,847
Thanks: 0
Thanked 11 Times in 11 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
Location: Bangalore, India
Posts: 2,847
Thanks: 0
Thanked 11 Times in 11 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
Registered User
 

Join Date: May 2007
Posts: 220
Thanks: 0
Thanked 0 Times in 0 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
Registered User
 

Join Date: Jul 2007
Posts: 145
Thanks: 0
Thanked 0 Times in 0 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
Registered User
 

Join Date: Jul 2007
Posts: 145
Thanks: 0
Thanked 0 Times in 0 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
How can i unzip .gz extension file? J_ang UNIX for Advanced & Expert Users 9 07-11-2009 01:08 PM
How to get file extension shirleyeow Shell Programming and Scripting 17 01-17-2008 07:40 AM
File System extension xramm Solaris 4 09-20-2007 08:48 AM
Stripping out the extension of a file name ramky79 Shell Programming and Scripting 2 12-27-2006 01:25 PM
default extension of file rujupriya UNIX for Dummies Questions & Answers 2 05-17-2006 10:49 AM



All times are GMT -4. The time now is 05:08 AM.