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.

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?

Thanks

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
2  
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
add comment

2 Answers

up vote 3 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
add comment

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
add comment

Your Answer

 
discard

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.