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 09-07-2008
Registered User
 

Join Date: Mar 2007
Location: Bangalore, India
Posts: 55
Thanks: 1
Thanked 1 Time in 1 Post
rename file extension

I am trying for loop to rename file extension from .txt to .html :

as below :

for i in *.txt; do mv "$i" `basename $i`.html; done
-------------------------------------------

But this renames a file file1.txt as file1.txt.html

anyone know how get avoid .html added after .txt ?

it should rename file.txt as file.html and not file.txt.html

Thanks in advance
Sponsored Links
    #2  
Old 09-07-2008
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 7,289
Thanks: 55
Thanked 427 Times in 408 Posts
The basename command gives the string after the last slash, try this:


Code:
for i in *.txt; do mv "$i" "${i%.txt}".html; done

Sponsored Links
    #3  
Old 09-07-2008
yahyaaa's Avatar
Registered User
 

Join Date: May 2008
Location: Jordan
Posts: 107
Thanks: 2
Thanked 0 Times in 0 Posts
Dear Franklin

what does this part "${i%.txt}".html mean ? the i% spesifically ?

Thanks
    #4  
Old 09-08-2008
Registered User
 

Join Date: Mar 2007
Location: Bangalore, India
Posts: 55
Thanks: 1
Thanked 1 Time in 1 Post
Thanks Franklin!

Yahyaa - its a pattern defined to exclude changes before .txt - "${i%.txt}".

Franklin - correct if i am wrong ...
Sponsored Links
    #5  
Old 09-08-2008
Moderator
 

Join Date: Feb 2007
Location: The Netherlands
Posts: 7,289
Thanks: 55
Thanked 427 Times in 408 Posts
Quote:
Originally Posted by yahyaaa View Post
Dear Franklin

what does this part "${i%.txt}".html mean ? the i% spesifically ?

Thanks
It strips the shortest match of ".txt" from back of $i.
Have a read of this tutorial regarding manipulating strings

Regards
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
extract file extension using sed r_praveenk Shell Programming and Scripting 7 09-09-2008 12:45 PM
Recursicely search and rename file extension riverside Shell Programming and Scripting 7 04-11-2008 10:02 AM
How to get file extension shirleyeow Shell Programming and Scripting 17 01-17-2008 07:40 AM
trim file name extension???? vishal_ranjan AIX 1 07-31-2007 09:34 AM
Help with multiple file rename - change case of part of file name steve7 UNIX for Dummies Questions & Answers 7 06-30-2005 01:41 PM



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