Omdøb filer med underlige navne

Sometimes you end up having files that can't be opened or renamed in Dolphin (re. KDE bug #165044) because their name contains silly characters in some silly encoding. This is what Dolphin might offer to do in such a situation... if it were able to detect that such a situation had occured (which it isn't, according to the heated discussion under the linked-to bug report)

Remove silly characters from every filename in the current directory? (~/bin/unfuck-names)

#!/bin/bash
for a in *.* 
do 
  b=$(echo "$a"|iconv -c) # strip characters that aren't valid under the default encoding
  if [ "$a" != "$b" ] # check if any characters have been removed
  then 
    c=1
    while [ -f "$b" ] # check if the new filename is vacant
    do 
      b="${b%.*}$c.${b##*.}" # generate a new name otherwise
      c=$(( c + 1 ))
    done
    mv -n "$a" "$b"
  fi
done

Tags: