I have this script that works with iconv to automatically convert the encoding of a group of files in a directory to UTF-8 and it changes the original files extention to .old but I want to know how to alter the script to make it look and convert all files in a directory AND all files in all subdirectories.
terminal code:
sudo convert/dir_iconv.sh convert/books cp1251 utf8
dir_iconv.sh script
#!/bin/bash
ICONVBIN='/usr/bin/iconv' # path to iconv binary
if [ $# -lt 3 ]
then
echo "$0 dir from_charset to_charset"
exit
fi
for f in $1/*
do
if test -f $f
then
echo -e "\nConverting $f"
/bin/mv $f $f.old
$ICONVBIN -f $2 -t $3 $f.old > $f
else
echo -e "\nSkipping $f - not a regular file";
fi
done
Instead of
for f in $1/*try using something likefor f in $(find $1 -type f). Additionally, the-type foption on the find command will skip non-file objects for you so thetestand conditional logic are not necessary.[edit]
For example this may work, totally untested (cleaned up the formatting a bit too) :