7

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.

1
  • 3
    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) Commented Jan 29, 2011 at 22:31

2 Answers 2

6

I found this just after I posted the question:

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

Works like a charm.

5

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .