Regex to batch rename files in OS X Terminal Regex to batch rename files in OS X Terminal shell shell

Regex to batch rename files in OS X Terminal


You can install perl based rename utility:

brew install rename

and than just use it like:

rename 's/123/onetwothree/g' *

if you'd like to test your regex without renaming any files just add -n switch


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


files = "*"for f in $files; do     newname=`echo "$f" | sed 's/123/onetwothree/g'`     mv "$f" "$newname"done