Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename with prefix/suffix?

How do I do mv original.filename new.original.filename without retyping the original filename?

I would imagine being able to do something like mv -p=new. original.filename or perhaps mv original.filename new.~ or whatever - but I can't see anything like this after looking at man mv / info mv pages.

Of course, I could write a shell script to do this, but isn't there an existing command/flag for it?

like image 230
Peter Boughton Avatar asked Oct 16 '08 11:10

Peter Boughton


People also ask

Is there a way to rename multiple files at once with different names?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.

What is a filename prefix?

The Filename Prefix field can be used to strip off a common prefix from image filenames, to enable the images to be linked to object accession numbers or system IDs.


1 Answers

You could use the rename(1) command:

rename 's/(.*)$/new.$1/' original.filename 

Edit: If rename isn't available and you have to rename more than one file, shell scripting can really be short and simple for this. For example, to rename all *.jpg to prefix_*.jpg in the current directory:

for filename in *.jpg; do mv "$filename" "prefix_${filename}"; done; 

or also, leveraging from Dave Webb's answer and using brace expansion:

for filename in *.jpg; do mv {,prefix_}"$filename"; done; 
like image 145
Simon Lehmann Avatar answered Oct 26 '22 18:10

Simon Lehmann