#!/bin/bash # renameLNX1 - Korn shell script from UVSI stored in: /home/uvadm/sf/util/ # renameLNX1 - rename an entire directory of filenames # - Deleting specified characters in filenames # echo "rename files in subdir - deleting specified characters" dir="$1"; chars="$2"; if [[ (-d "$dir") && (-n "$chars") ]]; then : else echo "usage: renameLNX1 directory 'characters' " echo " ==================================" echo " - arg1=directory, arg2=CharactersToDelete" echo " - must enclose characters in single quotes" echo " - may specify in hexadecimal" echo " example: renameLNX1 dirxx '\x20\x21\x27\x28\x29\x2C' " echo " ============================================" echo " - delete Space, Exclamation, SingleQuote, LeftParen, RightParen, Comma" exit 1; fi # reply="n" until [ "$reply" = "y" ] do echo "enter to rename files ? y/n" read reply done x=0; y=0 for dfn in $dir/* do fn1=${dfn##*/} fn2=$(echo $fn1 | tr -d $"$chars") #=========== see NOTE at end script ============= # echo "Debug: fn1=$fn1 fn2=$fn2" let x=x+1 if [[ $fn1 == $fn2 ]]; then continue; fi mv -i "$dir/$fn1" "$dir/$fn2" let y=y+1 echo "file# $x $dir/$fn1 - renamed to: $dir/$fn2" done echo "total $x files, $y renamed (deleting $chars)" exit 0 #------------- NOTE test/results ------------------ # # renameLNX1 dirxx '\x20\x21\x27\x28\x29\x2C' # --- input --- # file#1=comma=,leftparen=(,rightparen=),exclamation=! # file#2=blanks comma=, leftparen=(, rightparen=), exclamation=! # file#3-exclam=!,dollar=$,amp=&,quotes='",parens=(),star=*,comma=, # --- output --- # file#3-eclam=!,dollar=$,amp=&,quotes='",parens=(),star=*,comma=, # file#=blanks comma=, leftparen=(, rightparen=), eclamation=! <-- renameLNX1 NOT working # file#=comma=,leftparen=(,rightparen=),eclamation=! - $arg2 passed to tr ??? # #Note - characters 1,2,x are removed - why is HEX not recognized # - BUT it works if I hard code the 'tr' line as follows: # fn2=$(echo $fn1 | tr -d $'\x21\x24\x26\x27\x28\x29\x2A\x2C') # #=========================================================== #Note - see "renameLNX" alt script with HARD-CODED hex chars on 'tr', works OK