How can I rename all files in the current directory having a particular extension to another extension?

20,642

Solution 1

For your particular question this command will work. Verify that you are happy with the proposed renaming, then drop the -n to have the operation proceed.

rename -n 's/\.xvg$/.txt/' *.xvg

This basically means for all files with the .xvg extension in the current directory find the files that end ($) with .xvg and change that part of the name to .txt.

More detail and an example below.


Yes, the rename command will do this for you. A short example where I rename all of my files with the .html extension to .html_bak, ignoring other files in the directory.

I would strongly recommend using the -n command line option first (explained below) to see what changes would happen before executing the actual command.

What I start out with:

$ ls
a.html  a.txt  b.html  htmlfile

the -n means do a "trial run" .. just show what would change, don't change anything!

$ rename -n 's/\.html$/.html_bak/' *
a.html renamed as a.html_bak
b.html renamed as b.html_bak

Still have the original files.

$ ls
a.html  a.txt  b.html  htmlfile

Now for real:

$ rename  's/\.html$/.html_bak/' *

Files have been renamed.

$ ls
a.html_bak  a.txt  b.html_bak  htmlfile

In the above examples I use * to specify the files to be considered for the rename operation. I could have restricted this to .html files only with *.html.

For more information consult the rename man page

Solution 2

This can be done like this:

rename 's/xvg$/txt/' *.xvg

The glob pattern ensures that only *.xvg files are affected.

Share:
20,642

Related videos on Youtube

eikonal
Author by

eikonal

Updated on September 18, 2022

Comments

  • eikonal
    eikonal over 1 year

    Possible Duplicate:
    Renaming multiple files (changing extension)

    Suppose that in my current/working directory I have five files:

    file1.xvg
    file2.xvg
    file3.xvg
    file1.eps
    file2.eps
    

    Is there any way that I can rename all files in the current/working directory with the extension .xvg and give them the new extension .txt (leaving the .eps files alone)? Can this be done with rename, mv, or mmv perhaps? I am using Ubuntu Linux. Thanks!