extension changing

Posted on 2006-05-16
Last Modified: 2010-04-20
i want to change all the files' extensions to another one in an directory
with a shell prompt ..
it will be executed like c cpp and all the files' extensions will be converted from c to cpp??

thanx to your all..
Question by:btocakci
    LVL 23

    Assisted Solution

    Use the 'rename' command.  From the man page:

            rename - bulk rename files

            rename fromtext totext files...

            rename will rename the specified files by replacing the
            first occurrence of fromtext in their name with totext.

            For example, given the files foo1, ..., foo9, foo10, ..., foo278,
            the commands

              rename foo foo0 foo?
              rename foo foo0 foo??

            will turn them into foo001, ..., foo009, foo010, ..., foo278.


              rename .htm .html *.htm

            will fix the extension of your html files.
    LVL 19

    Accepted Solution

    changeext <old-ext> <new-ext>
    changeext c cpp

    Save this to /usr/local/bin/changeext and chmod it a+x

    for file in *.$1 ; do mv $file `echo $file | sed 's/\(.*\.\)$1/\1$2/'` ; done
    LVL 5

    Assisted Solution

    Or you could just skip the script, skip adding in extra variables, and just run a single line in the directory like this (there's plenty of ways to do what you're looking to do, it's just a matter of finding what you like best).

    $ ls                                                                                                                                      
    1.c  2.c  3.c  4.c  5.c
    $ ls | sed -n 's/\(.*\)c/mv \1c \1cpp/p' | bash                                                                                          
    $ ls                                                                                                                                      
    1.cpp  2.cpp  3.cpp  4.cpp  5.cpp

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 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

    24 Experts available now in Live!

    Get 1:1 Help Now