user370507 user370507 - 2 months ago 11
Bash Question

Regex to batch rename files in OS X Terminal

I'm after a way to batch rename files with a regex i.e.

s/123/onetwothree/g


I recall i can use awk and sed with a regex but couldnt figure out how to pipe them together for the desired output.

Answer

An efficient way to perform the rename operation is to construct the rename commands in a sed pipeline and feed them into the shell.

ls |
sed -n 's/\(.*\)\(123\)\(.*\)/mv "\1\2\3" "\1onetwothree\2"/p' |
sh