#!/bin/sh #----------------------------------------------------------- # Script name: mvb ("MV-Batch" -- in reference to the # *BSD/Linux command mv, which the script uses) # Written by: Steve Doonan, Portales, NM US # Email: xscd@xscd.com # Version: 1.5.5 # For the latest version, check: # http://www.xscd.com/pub/ # or email the author at the address above. Criticisms, # comments and suggestions are all welcome. # # This shell script was written to "batch rename" files # (change the name of many files at once) in the # current working directory. # # Change the path on the first line to point to the location # on your computer of either the Bourne shell (sh) or # the BASH (Bourne Again) shell (bash). At your shell's # command prompt, type: # which sh # or # which bash # to see where sh or bash is installed on your machine. # Be sure to retain the #! on the first line--just put # the correct path immediately after it, like: # #!/usr/local/bin/bash # Copy this script to a directory in your $PATH variable: # Type: # echo $PATH # to see the locations where executable programs reside # on your computer. # Then change to that directory and make this script # executable by typing: # chmod a+x mvb # # To use the script, change to the directory whose files # you would like to rename, then type the command mvb # followed by a space and the new name you would like # the files in that directory to have, then type RETURN # or ENTER # # FILE PERMISSIONS: If you would like for this script to # change the permissions for each file in addition to # renaming them, UNcomment the line (remove the # initial #) near the end that reads: # # chmod 664 "$I" # and change the permissions indicated in that command # to those appropriate for use on your own computer # --> refer to the man page: # man 1 chmod #----------------------------------------------------------- MVB_VERSION="1.5.6" if [ $# -eq 0 ] then cat << _EOF_ --------------------------------------------------------- You did not specify a NEWNAME for the files. After the name of the command please enter a SPACE followed by the name you would like all the files in the current directory to be renamed to. --------------------------------------------------------- _EOF_ exit 1 fi NEWNAME="$( echo "$*" | tr -c '[:alnum:]' '_' )" cat << _EOF_ --------------------------------------------------------- mvb (version ${MVB_VERSION}) RENAME FILES TO--> $NEWNAME CURRENT DIRECTORY--> $PWD Continue? (Press RETURN or ENTER) TO QUIT, type q (then press RETURN or ENTER) FOR INFORMATION, type i (then press RETURN or ENTER) --------------------------------------------------------- _EOF_ read CONTINUE case $CONTINUE in [!i]* ) exit ;; i ) more << _EOF_ --------------------------------------------------------------------- | INFORMATION for mvb | | | | This shell script (Bourne or BASH) will RENAME all visible files | | (files that don't begin with a dot) in the current directory, to | | a name you specify, appending a numerical index to each filename | | so that each is unique, and retaining the original filename | | extension, if one exists. This script will NOT rename | | subdirectories or symbolic links contained within the current | | directory, nor will it descend into subdirectories to rename | | files within them. | | | | ABOUT THE NEW FILENAME: The name you choose may include spaces | | (which will be converted to underscores by this script), but in | | general the new name should BEGIN with at least one alphabetic | | character and contain only letters, numbers, dots (periods) and | | underscores, as is usually recommended for any *BSD/Linux | | filename. | | | | If the script does not see what looks like a valid FILENAME | | EXTENSION (3-4 characters following a dot at the end of a | | filename) on the current name for each file, it will ask for | | one. If you WANT to add a filename extension, just type 3 or 4 | | characters (i.e. jpg, txt, html), with or without a preceding | | dot--the script will provide the dot if you do not. If you do | | NOT want the filename to have an extension, just press RETURN | | or ENTER at that prompt without typing any characters, and no | | filename extension will be added. | | | | FILE PERMISSIONS: If you would like for this script to change | | the permissions for each file in addition to renaming them, | | UNcomment the line (remove the initial #) near the end of the | | script that reads: | | | | # chmod 664 "\$I" | | | | and change the permissions to those appropriate for your own use | | --> refer to the man page: | | | | man 1 chmod | | | --------------------------------------------------------------------- To QUIT this program at any time, press CONTROL-C To CONTINUE, press RETURN or ENTER _EOF_ read REPLY ;; esac INDEX=0 FILES__NOT_ONLY_DIRS_AND_LINKS__FOUND="not-yet" make_zero_padded_index_number () { INDEX=$(( $INDEX + 1 )) INDEX_COUNT="$( echo "$INDEX" | tr -d '\n' | wc -c )" PADDING_ZEROS="$(ls "$PWD" | wc -l | tr '[:digit:]' '0' | tr -d '[:space:]' )" INDEX_ALPHANUMERIC="$( echo "${PADDING_ZEROS}${INDEX}" | cut -c$(($INDEX_COUNT + 1))- )" } echo "Renaming files..." echo for I in * do if [ "$I" = "*" ] #---------------------------------------------- # if no files are found in current directory... #---------------------------------------------- then cat << _EOF_ --------------------------------------------------------- No files were found. Script exiting... --------------------------------------------------------- _EOF_ exit 1 fi #----------------------------------------- # if file is NOT a directory or a link... #----------------------------------------- if [ -f "$I" -a ! -L "$I" ] then #----------------------------------------------- # if filename has a 3 or 4 character extension... #----------------------------------------------- if echo "$I" | grep "\.[^.0-9]\{3,4\}$" > /dev/null then #------------------------------------------------ # assign filename extension to variable EXTENSION #------------------------------------------------ EXTENSION="$( echo "$I" | sed 's/^.*\(\.[^.]*\)$/\1/' | tr [:upper:] [:lower:] )" #----------------------------------------------------- # otherwise, ask for a filename extension (or none)... #----------------------------------------------------- else echo echo '---------------------------------------------------------' echo "CURRENT FILE IS-->" $I echo "No (or improbable) filename extension found. Enter new" echo "filename extension or press RETURN or ENTER for none." echo -n "NEW FILENAME EXTENSION: " read NEW_EXTENSION echo '---------------------------------------------------------' echo #----------------------------------------------------------------- # cut the new extension (if any) down to no more than 4 characters #----------------------------------------------------------------- NEW_EXTENSION="$( echo "$NEW_EXTENSION" | sed 's/^\.*\(.\{0,4\}\).*$/\1/' \ | tr -d '\n' | tr -c '[:alnum:]' '_' | tr [:upper:] [:lower:] )" if [ -n "$NEW_EXTENSION" ] then EXTENSION=".${NEW_EXTENSION}" else EXTENSION='' fi fi #---------------------------------------------------------------------- # at this point, EXTENSION should be set correctly--an alphanumeric # index number is created (by the function make_zero-padded_index_number) # and the file is renamed (with a slightly different name if the computed # filename already exists in the current directory). #---------------------------------------------------------------------- make_zero_padded_index_number RENAME_TO="${NEWNAME}${INDEX_ALPHANUMERIC}${EXTENSION}" if [ -e "$RENAME_TO" ] then RENAME_TO="${NEWNAME}${INDEX_ALPHANUMERIC}a${EXTENSION}" fi # chmod 664 "$I" mv -iv -- "$I" "$RENAME_TO" FILES__NOT_ONLY_DIRS_AND_LINKS__FOUND="yes" fi done if [ "$FILES__NOT_ONLY_DIRS_AND_LINKS__FOUND" = "yes" ] then cat << _EOF_ --------------------------------------------------------- The files have been renamed. Script exiting... --------------------------------------------------------- _EOF_ else cat << _EOF_ --------------------------------------------------------- No files (only dir's or links) found. Script exiting... --------------------------------------------------------- _EOF_ fi