Search code examples
bashfilefor-looprenaming

Multiple file rename in bash.


Just starting out with Bash, and I am trying to write something to easily rename multiple files. The difficulty is that it's not just a simple append or changing of extension.

The files I am trying to name are from this format

IIS-1af24fa93f090177fe770e1213caf3a3-443.hex

To this format

1af24fa93f090177fe770e1213caf3a3

I have got something like this so far, but it returns errors

for f in ./* ; do mv "$f" "$(ls /root/download_logs/ | cut -d \- -f2)" ; done

Any help would be appreciated!

Thanks, Mike


Solution

  • Try something like this instead:

    for f in IIS-*.hex; do
      newname=${f%-*}
      newname=${newname#*-}
      mv "$f" "$newname"
    done