#!/bin/bash
#created by RDX 06.10.2008
cdir=$1
from_=$2
to_=$3
newname=""
if [[ "$cdir" = "--help" || "$cdir" = "-h" ]]
then
echo "fix invalid encodig of filenames"
echo "usage: drename <dir> [source_encoding destination_encoding]"
echo " default will be encode from CP1251 to UTF8"
exit 0
fi
#1 param - filename
#2 param - from ecoding
#3 param - to encoding
function to_other_converter()
{
newname=$(echo $1 | iconv -f${2} -t${3})
#if filename can be decoded
if [ $1 != $newname ]
then
#decode it
mv -f $1 $newname
fi
}
#1 param - filename
#2 param - from ecoding
#3 param - to encoding
function to_utf_converter()
{
#utf names shouldn't be renamed
is_in_to=$(echo $1 | gawk '/[а-яА-Я]/ {print $0}')
if [ "$is_in_to" = "" ]
then
to_other_converter $1 $2 $3
fi
}
#default values
if [ "${from_}" = "" ]
then
from_=CP1251
fi
if [ "${to_}" = "" ]
then
to_=UTF-8
function curent_converter()
{
to_utf_converter $1 $2 $3
}
else
function curent_converter()
{
to_other_converter $1 $2 $3
}
fi
#field separator must be 'enter' for filenames with spaces
IFS='
'
# for each item in directory
for fname in $(ls $cdir)
do
curent_converter $fname $from_ $to_
#recursive tree traversal of directories
if [ -d $newname ]
then
cd $newname
drename . $from_ $to_
cd ..
fi
done
echo $(pwd) renamed
Перебирает рекурсивно все подкаталоги. И у всех файлов у которых может поменять кодировку в названии - меняет.
Комментариев нет:
Отправить комментарий