Anyone plz tell me the command to rename file extensions in unix..
That command should be other than:
mv sample.txt sample.html
Printable View
Anyone plz tell me the command to rename file extensions in unix..
That command should be other than:
mv sample.txt sample.html
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..
That's aweful programming!!!
What that command does is,
[CODE]mv /user/abcde1/v/first.txt /user/abcde1/v/first.html[/CODE]
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/'`
[/CODE]
[B]$file[/B] will have the value [B]/user/abcde1/v/first.txt[/B].
Whichever string you give inside backquotes, will be considered a command and executed.
So, [CODE]`echo $file | sed 's/\(.*\.\)txt/\1html/'`[/CODE] is considered as a command.
[B]echo $file[/B] will print the value of $file into the buffer. Since we are using pipe, [B]sed [/B]will act on this data (i.e. the value of $file).
[B]sed[/B] is an editor. and 's' stands for substitution. It should be simple now to get what the command is doing.:D
But, it is seriously dumb programming if one chooses this command instead of [CODE]mv filename.ext filename.newext[/CODE]. Why would you want to get into this complication?
Thank you.
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 [B]sed[/B] read this.
[url=http://www.grymoire.com/Unix/Sed.html]Sed - An Introduction and Tutorial[/url]
Thats so useful. thanks a lot.