Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory containing a bunch of files with different extensions, but the filename before the extension is all the same. E.g., file1.txt, file1.jpg, file1.pdf, file1.odt...

I want to change the 'file1' part, but not the extension, so the result would be, e.g., newfilename.txt, newfilename.jpg, newfilename.pdf, newfilename.odt

I can't figure out how to do this without tediously running mv file1.txt newfilename.txt on every one. I can find lots of tuturials online to change file names with brace expansion if you know all the parts to expand, but nothing to just replace file1 with newfilename no matter what the extension. Is this possible, or am I barking up the wrong tree?


EDIT: I'm sorry, not moments after posting this I found a different page in my Google results that answered the question for me: for f in file1.*; do mv "$f" "${f/file1/newfilename}"; done works perfectly.

share|improve this question
if you find an answer to your own question, you should post it as answer such that other can vote on it separately (yes, you can answer your own questions) – maxschlepzig Jan 29 '11 at 22:31
@max: Thanks, I didn't know that. – Wolf Jan 29 '11 at 22:58
up vote 4 down vote accepted

I found this just after I posted the question:

for f in file1.*; do mv "$f" "${f/file1/newfilename}"; done

Works like a charm.

share|improve this answer

You can do that with bash, bu there are other tools more suited for the job.

On most distros:

rename file1 newfilename file1.*

On Debian and Ubuntu, replace rename with rename.ul.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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