Bash – using brace expansion to change filenames, not extensions

bashshell

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.

Best Answer

I found this just after I posted the question:

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

Works like a charm.

Related Question