extension changing

Posted on 2006-05-16
Last Modified: 2010-04-20
hi!i need to write a shell script which changes the extensions of files in a directory.
For example, if the name of the script is then a call such as below c cpp
changes the extensions of files which are "c" to "cpp"..

Need your help, thanks..
Question by:btocakci
    LVL 45

    Accepted Solution

    Hi btocakci,

    find . -type f -name "*.$1" | sed "s:\(.*\)\.$1:\1:" | while read filename
          mv $filename.$1 $filename.$2

    LVL 12

    Expert Comment

    FWIW, you can do this without a script as well

    rename .c .cpp *.c

    or for another example

    rename .txt .text *.txt


    Author Comment

    hi sunnycoder!
    which variable is c and which one is cpp??
    how can adapt your command as i want? (c to cpp)
    LVL 45

    Expert Comment

    > c cpp
    >changes the extensions of files which are "c" to "cpp"..
    $1 is the first argument ... c in your example ... $2 is the second argument ... cpp in your example

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now