#!/bin/bash # renameOKx - Korn shell script from UVSI stored in: /home/uvadm/sf/util/ # renameOKx - rename directory of filenames <-- delete chars in HEX, does NOT work ??? # - Deleting all LowBit (except LineFeed) & HighBit characters # renameOK <-- Alternate version specifies delete chars in OCTAL & does work # renameOKx <-- This script specifies delete chars in HEX, but does NOT work - WHY NOT ??? # --> see test input/output at bottom #Note - must use 'bash' shell for HEX to work at all, but complement option not working in hex # echo "$0 - rename all files in directory Deleting LowBit (except LineFeed) & HighBit characters" dir="$1"; if [[ ! -d "$dir" ]]; then echo "usage: renameOKx directory " echo " ===================" echo " - arg1 must be a directory" 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 -cd '\12\40-\176') #<-- octal obsolete & stupid fn2=$(echo $fn1 | tr -cd \$'\x0A\x20-\x7E') #<-- hex preferred, but has bugs ? #Note - option 'c' of '-cd' COMPLEMENTS (deletes chars not specified) # - does NOT work if chars specified in HEX, but OK if OCTAL - WHY diff ??? # 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 LowBit (except LineFeed) & HighBit characters" exit 0 # renameOKx tmp1 # ============== # # Original filenames in directory tmp1 # -rw-r----- 1 uvadm apps 84335 Nov 5 08:09 Another.jpg # -rw-r----- 1 uvadm apps 1500403 Nov 5 08:10 IMG_20170121_142512.jpg # -rw-r----- 1 uvadm apps 48151 Nov 5 08:11 Mia - tomato% +head art.jpg # -rw-r----- 1 uvadm apps 92384 Nov 5 09:11 My art: at^ _vsb |offices.jpg # # Resulting filenames after --> renameOKx tmp1 # -rw-r----- 1 uvadm apps 84335 Nov 5 09:14 Anotherjpg # -rw-r----- 1 uvadm apps 1500403 Nov 5 09:14 IMG_20170121_142512jpg # -rw-r----- 1 uvadm apps 92384 Nov 5 09:14 Mart:at^_vsbofficesjpg # -rw-r----- 1 uvadm apps 48151 Nov 5 09:14 Miatomatoheadartjpg # # renameOK tmp1 <-- Alternate script specifies delete chars in Octal works OK # ============= - why don't renameOKx HEX chars work ???