Tag Archives: linux bash umlauts

renaming umlauts with bash-script

hey guys,

have had troubles with umlauts … so, quick and dirty, i’ve written some lines for the bash:

#! /bin/bash
# rnUmlauts.sh

ONE=1
number=0
EXPECTED_ARGS=2

if [ $# -ne $EXPECTED_ARGS -o \( “$1” != “–test” -a “$1” != “–force” \) ]
then
echo “NAME”
echo “\trnUmlauts – Renaming files with umlauts\n”
echo “Synopsis”
echo “\t`basename $0` –force|–test pathname\n”
echo “Examples”
echo “\t`basename $0` –test /usr/share”
echo “\t\tonly shows which files will be renamed in the directory /usr/share\n”
echo “\t`basename $0` –force /etc/doIt”
echo “\t\trenaming the files with umlauts in the directory /etc/doIt\n”
exit 1
fiif [ -d $2 ]; then
echo “Searching for files with umlauts in $2 …”
else
echo “Directory $2 doesn’t exist!\n”
exit 2
fi

files=$(find $2 -name *[äöüÄÖÜß]* > .temp)

while read file;do
n=`echo $file | sed -e “s/ä/ae/g”`
n=`echo $n | sed -e “s/ö/oe/g”`
n=`echo $n | sed -e “s/ü/ue/g”`
n=`echo $n | sed -e “s/Ä/Ae/g”`
n=`echo $n | sed -e “s/Ö/Oe/g”`
n=`echo $n | sed -e “s/Ü/Ue/g”`
n=`echo $n | sed -e “s/ß/ss/g”`
if [ “$1” = “–test” ]
then
echo “$file will be renamed into $n”
else
echo “Renaming $file to $n”
mv “$file” “$n”
fi
number=$(($number+1))
done<.temp

rm .temp

if [ “$1” = “–test” ]
then
if [ “$number” -eq “$ONE” ]
then
echo “$number file will be renamed.”
else
echo “$number files will be renamed.”
fi
else
if [ “$number” -eq “$ONE” ]
then
echo “$number file renamed.”
else
echo “$number files renamed.”
fi
fi

exit 0

maybe you can use it, too.

best regards!