Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

renaming file extensions..

This is a discussion on renaming file extensions.. within the Unix/Linux forums, part of the Operating Systems category; Anyone plz tell me the command to rename file extensions in unix.. That command should be other than: mv sample.txt sample.html...

Go Back   Geeks Talk > Operating Systems > Unix/Linux
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 12-18-2006
Expert Member
 
Join Date: Dec 2006
Location: Chennai
Posts: 210
Thanks: 2
Thanked 17 Times in 15 Posts
Barbie is on a distinguished road
renaming file extensions..

Anyone plz tell me the command to rename file extensions in unix..
That command should be other than:
mv sample.txt sample.html
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-18-2006
Expert Member
 
Join Date: Dec 2006
Location: Chennai
Posts: 210
Thanks: 2
Thanked 17 Times in 15 Posts
Barbie is on a distinguished road
Re: renaming file extensions..

file='/user/abcde1/v/first.txt'
mv $file `echo $file | sed 's/\(.*\.\)txt/\1html/'`

I got this command to rename extension and it works correctly.
But i don know how it does(the actual functionality).
help me out. plz..
Reply With Quote
  #3 (permalink)  
Old 12-18-2006
Expert Member
 
Join Date: Sep 2006
Location: Bengalooru (Formerly called Bangalore), India
Posts: 486
Thanks: 2
Thanked 29 Times in 28 Posts
kalayama is on a distinguished road
Re: renaming file extensions..

That's aweful programming!!!
What that command does is,
Code:
mv /user/abcde1/v/first.txt /user/abcde1/v/first.html
Here's how it works.
Code:
file='/user/abcde1/v/first.txt'  //Assign the filename to variable file
mv $file `echo $file | sed 's/\(.*\.\)txt/\1html/'`
$file will have the value /user/abcde1/v/first.txt.
Whichever string you give inside backquotes, will be considered a command and executed.

So,
Code:
`echo $file | sed 's/\(.*\.\)txt/\1html/'`
is considered as a command.

echo $file will print the value of $file into the buffer. Since we are using pipe, sed will act on this data (i.e. the value of $file).

sed is an editor. and 's' stands for substitution. It should be simple now to get what the command is doing.

But, it is seriously dumb programming if one chooses this command instead of
Code:
mv filename.ext filename.newext
. Why would you want to get into this complication?
__________________
[COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Reply With Quote
  #4 (permalink)  
Old 12-18-2006
Expert Member
 
Join Date: Dec 2006
Location: Chennai
Posts: 210
Thanks: 2
Thanked 17 Times in 15 Posts
Barbie is on a distinguished road
Re: renaming file extensions..

Thank you.
Reply With Quote
  #5 (permalink)  
Old 12-19-2006
Expert Member
 
Join Date: Sep 2006
Location: Bengalooru (Formerly called Bangalore), India
Posts: 486
Thanks: 2
Thanked 29 Times in 28 Posts
kalayama is on a distinguished road
Re: renaming file extensions..

You are most welcome.
I guess you do know about sed. sed is (one of) the best stream editor. It comes in handy in a lot of occassions.
To explore the possibilities with sed read this.
Sed - An Introduction and Tutorial
__________________
[COLOR="Blue"][SIZE="2"]"If you are not living on the edge of your life, you are wasting space"[/SIZE][/COLOR]

Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Reply With Quote
  #6 (permalink)  
Old 12-19-2006
Expert Member
 
Join Date: Dec 2006
Location: Chennai
Posts: 210
Thanks: 2
Thanked 17 Times in 15 Posts
Barbie is on a distinguished road
Re: renaming file extensions..

Thats so useful. thanks a lot.
Reply With Quote
Reply

  Geeks Talk > Operating Systems > Unix/Linux

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
Listen to any PDF file!!! Shivanna Geeks Lounge 8 4 Weeks Ago 08:19 AM
How to store a File in Databes? chowsys VB.NET 2 12-26-2007 04:42 AM
Can we edit the file fred Windows 1 09-13-2006 12:06 PM
Is this command serve the purpose of renaming christia Unix/Linux 1 09-09-2006 04:18 PM
C Programming - File management in C Lokesh M C and C++ 0 05-31-2006 03:35 PM


All times are GMT -4. The time now is 01:17 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved