Search code examples
regexmacosterminalrenamefile-rename

Move end of filename to beginning on OSX El Capitan


I've got a few thousand files in a few folders with names like so:

filename #1.mp4
filename #1.mp4
.
.
.
filename #5555.mp4

Is there a way to move the #number part to the beginning of the filenames in bulk, on a mac? So the result would look like:

 #1 filename.mp4
 #2 filename.mp4
 .
 .
 .
 #5555 filename.mp4

So far I've tried two pieces of software, Renamer for Mac and Namechanger for Mac. Neither have the option to do what I'm looking for.


Solution

  • Using rename utility (perl based) you can do:

    rename 's/^(.+)\s+(#\d+)/$2 $1/' *.mp4
    

    If rename is not available then use sed:

    for f in *.mp4; do
        mv "$f" "$(sed -E 's/^(.+) +(#[0-9]+)/\2 \1/' <<< "$f")"
    done