A function that funnels all filenames into a file and opens that file in vim. The user then changes the names, saves, and quits. Finally the function renames the files in the folder with the new names provided in the file.

This is the update to this previous version. The update makes it impossible to destroy files, and improves performance. This is the answer that helped develop this version.

bulkrename() {
    trap 'rm -f $RENAMETMP' EXIT

    # Make array of file names
    local OLDFILENAMES=( *$@ )

    # Pipe array items into file seperated by new lines
    local RENAMETMP=$(mktemp)
    printf '%s\n' "${OLDFILENAMES[@]}" > $RENAMETMP && nvim $RENAMETMP

    # If user exits nvim with failure, stop the script
    local exitcode=$?; [[ $exitcode = 0 ]] || return $exitcode

    # Place new names into an array
    local IFS=$'\n'
    local NEWFILENAMES=( $( command cat $RENAMETMP ) )

    # After user has edited the names, check for matching line count
    if [[ ${#OLDFILENAMES[@]} -ne ${#NEWFILENAMES[@]} ]]; then
        echo "Lines Mismatch" && return 1

    # Also check for naming conflicts
    for ((old = 1; old <= ${#OLDFILENAMES[@]}; old++)); do
        for ((new = 1; new < $old; new++)); do
            if [[ ${NEWFILENAMES[old]} = ${NEWFILENAMES[new]} ]] || \
            [[ ${OLDFILENAMES[old]} = ${NEWFILENAMES[new]} ]]; then
                echo "File Naming Confict" && return 1

    # Rename the files
    for ((name = 1; name <= ${#NEWFILENAMES[@]}; name++)); do
        command mv -Tnv "${OLDFILENAMES[name]}" "${NEWFILENAMES[name]}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.