Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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!

share|improve this question

marked as duplicate by jasonwryan, Mat, Kevin, Gilles, Michael Mrozek Jul 2 '12 at 7:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
add comment

This can be done like this:

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

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

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.