epcim
10/20/2015 - 11:34 AM

filename_cleaner.howto.md

simple file name cleaner (there is also version 1 in gitst)

#!/bin/bash
if [ $# -eq 0 ];then
  echo "Usage: $0 directory ..."
else
  while [ $# -gt 0 ];do
    if [ -d "$1" ];then
      find "$1" -mindepth 1 -maxdepth 1 | while read name;do
        #nname="$( echo "$name" | tr 'ĄąÁÂĂÄáâăäĆÇČćçčĎĐďđÉĘËĚéęëěÍÎíłľĹĺŃŇńňÓÔŐÖóôőöŔŕŘřŚŠŞśšşŤťŢţŮÚŰÜůúűüÝýŹŽŻźžżšžŠŽ' 'AaAAAAaaaaCCCcccDDddEEEEeeeeIIiiLLllLlNNnnOOOOooooRrRrSSSsssTtTtUUUUuuuuYyZZZzzztszTSZ' | tr 'A-Z' 'a-z' | tr -cs -- '-0-9a-z_.' '_' | tr -s -- '-_' '-_' | sed 's/^[-_]\+//;s/[-_]*-[-_]*/-/g;s/[-_]\+\.\/./g;s/\.[-_]\+/./g;' )"
        nname="$( echo "$name" | tr 'ĄąÁÂĂÄáâăäĆÇČćçčĎĐďđÉĘËĚéęëěÍÎíłľĹĺŃŇńňÓÔŐÖóôőöŔŕŘřŚŠŞśšşŤťŢţŮÚŰÜůúűüÝýŹŽŻźžżšžŠŽ' 'AaAAAAaaaaCCCcccDDddEEEEeeeeIIiiLLllLlNNnnOOOOooooRrRrSSSsssTtTtUUUUuuuuYyZZZzzztszTSZ' | tr -cs -- '-0-9a-z_.' '_' | tr -s -- '-_' '-_' | sed 's/^[-_]\+//;s/[-_]*-[-_]*/-/g;s/[-_]\+\.\/./g;s/\.[-_]\+/./g;' )"
        if [ "$name" != "$nname" ];then 
          if [ -e "$nname" ];then
            i=0
            oname="$nname"
            while [ -e "$nname" ];do nname="$( printf "%s.%05i" "$oname" $i )";i=$[$i+1];done
          fi
          echo mv -v "$name" "$nname"
        fi
        find "$1" -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0r "$0"
      done
      shift
    else
      echo "Directory $1 does not exist"
    fi
  done
fi

Alternatives

ICONV

srcenc=latin2
while [ -n "$1" ]
do
   n="$(iconv -f "$srcenc" -t "ascii//TRANSLIT" <<< "$1" )"
   mv "$1" "$n"
   rename 's/ +/_/g;y/A-Z/a-z/' "$n"
   shift 1
done

SED

nname="$( echo "$name" | sed  'y/ĄąÁÂĂÄáâăäĆÇČćçčĎĐďđÉĘËĚéęëěÍÎíłľĹĺŃŇńňÓÔŐÖóôőöŔŕŘřŚŠŞśšşŤťŢţŮÚŰÜůúűüÝýŹŽŻźžż\x9D\x9A\x9E\x8D\x8A\x8E/AaAAAAaaaaCCCcccDDddEEEEeeeeIIiiLLllLlNNnnOOOOooooRrRrSSSsssTtTtUUUUuuuuYyZZZzzztszTSZ/;y/A-Z/a-z/;y/-0-9a-z_./_/cs;y/-_/-_/s;s/^[-_]\+//;s/[-_]*-[-_]*/-/g;s/[-_]\+\.\/./g;s/\.[-_]\+/./g;' )"