#!/bin/sh ############################################################################## # Rename 0.1 (beta) ############################################################################## # Author: Adam Carheden, 4/14/2000 ############################################################################## # Licence: GNU GPL # Copy, modify, sell, etc., so long as others can do the same. ############################################################################## # Description: # Renames files from STDIN based on tr character translation # Default behavior is to rename files by translating upper case characters to # lower case characters ############################################################################## # Examples: # - Rename all files in the current directory to all lowercase letters # ls | rename # - Rename all files in the current directory to all uppercase letters # ls | rename -u # - Test the script (Does everything but call mv) # ls | rename -t # - Rename only files that start with an upper case character from A to C # ls | rename --regexp '^[A-C].*' # - Rename files so that all occurences of 'A' are replaced with 'B' # ls | rename --chars A B ############################################################################## # Defaults myName="$0" testOnly='' verbose='' regexp='^\.\.\?$' # Refuse to rename std dir pointers grepNeg='-v' usage="Usage: $0 -h | --help) Print this message and exit -v | --verbose) Verbose output -c | --chars FROM TO) Rename based on FROM and TO (see man tr) -t | --test) Test only, skip actual renaming -e | --rexexp REGEXP) Only rename files if the match the regular expression -u | --uppercase) Translate to all uppercase" from='[:upper:]' to='[:lower:]' # Get Arguments while [ $1 ] do case $1 in --help|-h) echo -e "${usage}" exit 0 ;; --verbose|-v) verbose=$1 shift ;; --chars|-c) shift from="$1" shift to="$1" shift ;; --test|-t) testOnly=$1 shift ;; --regexp|-e) grepNeg='' shift regexp=$1 shift ;; --uppercase|-u) from='[:lower:]' to='[:upper:]' shift ;; *) echo -e "${usage}" exit 1 ;; esac done # Make sure we have translation operators and a regexp if [ -z "${from}" ]; then echo -e "${usage}" echo "FROM empty" 1>&2 exit 1 fi if [ -z "${to}" ]; then echo -e "${usage}" echo "TO empty" 1>&2 exit 1 fi if [ -z "${regexp}" ]; then echo -e "${usage}" echo "REGEXP empty" 1>&2 exit 1 fi # Read lines and rename read nextFile more=`echo $?` while [ "${more}" -eq 0 ]; do # Test that the file should be included if [ `echo -n "${nextFile}" | grep ${grepNeg} -q -e "${regexp}"; echo $?` -eq 0 ]; then # Test that file exists if [ -r "${nextFile}" ]; then # Get the new name newName=`echo -n "${nextFile}" | tr ${from} ${to}` # Text for duplicate file name if [ "${nextFile}" != "${newName}" ]; then # Make sure the target file doesn't exist if [ -r "${newName}" ]; then echo "${myName}: A file called '${newName}' already exists, Skipping..." 1>&2 else # Rename the file if [ -n "${verbose}" ]; then echo "Renaming '${nextFile}' to '${newName}'" fi if [ -z "${testOnly}" ]; then if [ `mv "${nextFile}" "${newName}"; echo $?` -gt 0 ]; then echo "${myName}: Error renaming '${nextFile}' to '${newName}'" 1>&2 fi fi fi else # Report duplicate file name error if [ -n "${verbose}" ]; then echo "'${newName}' = '${nextFile}', Skipping..." fi fi else echo "${myName}: '${nextFile}' does not exist. Skipping..." 1>&2 fi else echo "${myName}: '${nextFile}' doesn't match criteria, Skipping..." 1>&2 fi # Get the next file read nextFile more=`echo $?` done # Indicate testing if [ -n "${test}" ]; then if [ -n "${verbose}" ]; then echo "Testing only, file names not modified." fi fi exit 0