Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append same string in many files names in mac at once?

I just need to append the string "eng" to many files names in same directory without changing its extension in MAC TERMINAL. I searched it for long time, i found mv command to rename files at once. But i don't know how to implement in to my scenario. Can any ony guide me ?

Thanks

like image 208
VivekParamasivam Avatar asked Nov 28 '22 15:11

VivekParamasivam


1 Answers

If you have a directory containing the following files:

a.ext
b
c.long.sh

And you want to rename them to:

aeng.ext
beng
c.longeng.sh

The following "oneliner" in a Mac terminal (bash) should do it:

for i in *; do name="${i%.*}"; mv "$i" "${name}eng${i#$name}"; done

To explain:

  • for i in *; do - iterates over all your filenames
  • name="${i%.*}" - extracts the name portion before the extension (strips off everything past the last dot)
  • mv - handles the rename
  • ${name}eng - adds eng to the name
  • ${i#$name} - gets the extension (this strips the name portion from the original filename)

Note: If you want to preview what it would do, but not actually perform the rename, insert an "echo" before the "mv". This will print the statements to the screen instead of executing the rename.

like image 65
Oli Avatar answered Dec 06 '22 09:12

Oli