Bash – rename middle name of multiple files using bash

bashrename

I've multiple files located in /opt/rec/ which I want to rename only some part of it using bash.

Original file name:

WK6LZTPR99999999_dig_2017-07-10 01:55:57.xy

which I want to change all files with in that directory as:

WK6LZTPR99999999_cur_2017-07-10 01:55:57.mp3

Best Answer

Here is bash solution.

for file in /path/to/*; do 
    nname="${file%%.*}.mp3"  # strip last part of file till first . seen
    echo mv "$file" "${nname//dig/cur}"  # replace 'dig' with 'cur'
done

With mmv, it's much easier.

mmv '*_*_*.*' '#1_cur_#3.mp3'

Or with zmv:

zmv -w '*_*_*.*' '$1_cur_$3.mp3'
Related Question