macosunixcommand-line-tool

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


Solution

  • 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:

    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.