一例批量轉換目錄下文件編碼的shell腳本代碼。
需求描述:
由于從window轉linux過來,很多原來win下的gbk文件需要轉換成utf8。
以下腳本僅判斷非utf8文件轉換成utf8文件,并且默認非utf8文件為gbk,如果文件類型不一致需要修改。
例子:
代碼如下:
#!/bin/bash
# File Name: iconv.sh
# Author: wanggy
# site: www.jb51.net
#
show_file()
{
for file in `ls $1`
do
if [ -d $1"/"$file ];then
#目錄遞歸調用show_file函數
show_file $1"/"$file
else
#文件
echo $1"/"$file
file_type=`file $1"/"$file`
type=`echo $file_type |grep UTF-8`
if [ -z "$type" ];then
echo "為空非utf-8編碼,轉換"
iconv -f gbk -t utf8 $1"/"$file -o $1"/"$file
else
echo "utf8編碼不用轉換"
fi
fi
done
}
path=./shell
show_file $path
新聞熱點
疑難解答