Assisting 13807015 developers since 2005.

Web Developer, Open Source, Loyal, Dad.  

Read More »

How to Rename File Extensions & Variables in Linux

Posted by David  •  Jun 16th, 2014 5:37:30 pm - Subscribe | Mood: good | Music: No Music

This entry explains how to rename files extensions & variables in Linux.

Currently I have a web server running CentOS. Recently I came across a ton of files in various directories with a file extension .html which needed to be renamed to .php. (I configured .htaccess to not be allowed on the server)

Rename File Extensions in Linux


Things you will need:
  • Terminal
  • Vi

    First you will need to do is open up terminal and navigate to the parent directory in which all of your files with the file extensions that you would like to change are located. Once at this location run the code that I have provided for you below. What this will do is find and rename all files with the extension .html to .php regardless of the name of the file. So you will be responsible for modifying my .html and .php to the extensions that suit your change.

    quote:
    find . -depth -name "*.html" -exec sh -c 'mv "$1" "${1%.html}.php"' _ {} \;


    Now then, if you would like to specify the path and NOT have to navigate to the path, simply change the /var/www/html/mydir in the line below to the path with the files with the extensions that need to be renamed. Also be sure that you change my .html and .php to the extensions that suit you!

    quote:
    find /var/www/html/mydir -depth -name "*.html" -exec sh -c 'mv "$1" "${1%.html}.php"' _ {} \;


    No need to contine below if this completes your needs:

    Now that we have all of our file extensions changed from .html to .php in the current directory and subdirectories all we need to do is rename the contents in our index.php formelly index.html. If we we're to view index.php in our browser we would have a bunch of links pointing to .html files, but since we renamed them the links now 404. sad.gif

    Well let's update those links! Open index.php (or whatever your index is) in vi, and enter the following:

    quote:
    :%s/.html .php


    This will now update all .html extension to .php that are found inside of the file index.php. Keep in mind that many of you will not have to do this, and your renaming stopped after step one in this entry.

    Well, I hope this helped you with the pain staking task of renaming files on your linux machine. Yes, I do know that my instructions can be hard to follow if you are a beginner to the linux system. But when I have time, I promise to create a video that will demonstrate everything that I have explained above.

    As always if you have any questions leave a comment, I promise to assist you in any way I can.

    Comments 0  •  Jun 16th, 2014 5:37:30 pm - Subscribe  •  Tweet this entry | Post a comment



  • Responses

    Leave a Reply


    ReCaptcha:

    Posting as anonymous Anonymous guest, why not register, or login now.

    Image Gallery

    Badges

    Amazon Wishlist Stumble Upon Toolbar
    Internet Blogs - Blog Top Sites Internet Blogs - Blog Top Sites Technology

    About Me

    avatar

    @davidpolanco
    iPhone: 29.513522,-98.565523 Web Designer, Open Source, Loyal, Dad. Learn more...