Aug 122007
 

Sponsored Link

Let us say that you want to rename all of your “.php5″ files to “.php” files. You can use for loop.


for old in *.php5; do cp $old `basename $old .php5`.php; done

Thats all there is to it. Let us say you need to rename index.php5 to index.php. The way above snippet works is that it loops through current directory and finds all the files with extension “.php5″ and processes ‘one by one. In our index.php5 file example, it finds index.php5 file, does a cp index.php5 `basename index.php5 .php5`.php <- basename returns “index” so you add .php to it and now you are left with index.php. Of course you can do mv command if you want to just move the file to new name.

 Posted by at 5:06 pm

  12 Responses to “Rename multiple files to another extension in Linux”

  1. ich mag prename lieber …

  2. You can also use the ‘rename’ command included with most debian installs. It’s a little more intuitive, imho.

    $ rename
    Usage: rename [-v] [-n] [-f] perlexpr [filenames]

    $ rename ‘s/\.php5/.php/’ *.php5

  3. And I’m using ‘mmv’ ;-)

  4. And this can be usede too:

    #!/bin/bash
    for x in *.php5; do n=${x/.php5/.php}; mv $x $n; done

    And it’s can be used in other dir than ‘.’

  5. The same can be done using the parameter expansion feature of the bash shell.

    for a in *php5 ; do mv $a ${a%%5} ; done

  6. Good job!

  7. What all those commands do not cover is to add an extension to a file that has none.
    How would that work?

  8. Vincent,

    for old in *; do mv $old `basename $old`.zip; done

    * = all files, you can change that though. like FILE*

  9. how would you keep the extension but append -xyz to the basename?
    for example, i want to copy all files in dir a and append -xyz the end of the name while keeping the extension to dir b.

  10. sorry to raise a dead thread, if anyone cares, but I’ve got a stumper for ya.

    How would you do a batch rename of a directory full of files some of which contain spaces in their names?

    for example:

    file 1.jpeg
    file 2.png
    file 3 and extra wording.jpeg
    completely-arbitrary-filename without extension

    to:
    file 1.TXT
    file 2.TXT
    file 3 and extra wording.TXT
    completely-arbitrary-filename without extension.TXT

    (And yes, I know this is a silly example, but it’s for a RL-practical use.)

    Thanks!
    -e.m.fields
    chapel hill, nc

  11. i want all extension of open office in linux operting system…?

  12. very useful thanks!

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>