Results 1 to 6 of 6

Thread: renaming file extensions..

  1. #1
    Expert Member
    Join Date
    Dec 2006
    Answers
    204

    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


  2. #2
    Expert Member
    Join Date
    Dec 2006
    Answers
    204

    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..


  3. #3
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    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!"

  4. #4
    Expert Member
    Join Date
    Dec 2006
    Answers
    204

    Re: renaming file extensions..

    Thank you.


  5. #5
    Expert Member
    Join Date
    Sep 2006
    Answers
    477

    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!"

  6. #6
    Expert Member
    Join Date
    Dec 2006
    Answers
    204

    Re: renaming file extensions..

    Thats so useful. thanks a lot.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact