How can I batch move a prepended year to the end of a file name? How can I batch move a prepended year to the end of a file name? unix unix

How can I batch move a prepended year to the end of a file name?


Try rename command:

rename -n 's/(.*) - (.*)(\.txt)/$2 ($1)$3/' *.txt

-n(--no-act) option is for preview.
Remove -n to perform substitution.


Untested.

find /path -name '???? - *.txt' -print0 | while read -d ''; do    [[ $REPLY =~ (.*)/(....)\ -\ (.*)\.txt$ ]] || continue    path=${BASH_REMATCH[1]}    year=${BASH_REMATCH[2]}    str=${BASH_REMATCH[3]}    echo mv "$REPLY" "$path/$str ($year).txt"done

Remove the echo once the generated mv commands look right.


I know you didn't tag it with zsh but you did say shell. Anyway here's how to do it with the zmv function in zsh:

autoload zmv                      # It's not loaded by defaultzmv -nvw '* - *.*' '$2 ($1).$3'

Remove -n when you're happy with the output.

-v makes zmv verbose. -w implicitly makes a group of each wildcard.