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

2

6

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

Homeric answered 19/6, 2014 at 8:14 Comment(2)
So you want to append "eng" to the file contents or filename?Shivery
Sorry to append it to the file namesHomeric
H
28

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.

Harken answered 19/6, 2014 at 8:39 Comment(0)
T
0

While this question specifically asks for "Mac Terminal" this can also be done in Mac Finder as follows:

  • Select all files in the directory (Command + A)
  • Right click on one of the selected files
  • Select Rename
  • Select "Add Text" and "after name"
  • Put eng in the text box
  • Click Rename

This will put the characters "eng" after the filename, but still keep the extension intact.

Tyr answered 3/10, 2024 at 16:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.