Skip to content

Instantly share code, notes, and snippets.

@GiaoGiaoCat
Created September 28, 2016 06:18
Show Gist options
  • Save GiaoGiaoCat/96b4865cf99e2375955859a669458209 to your computer and use it in GitHub Desktop.
Save GiaoGiaoCat/96b4865cf99e2375955859a669458209 to your computer and use it in GitHub Desktop.
批量转换文件编码
#!/bin/bash
DIR=$1 # 转换编码文件目录
FT=$2 # 需要转换的文件类型(扩展名)
SE=$3 # 原始编码
DE=$4 # 目标编码
for file in `find $DIR -type f -name *.$FT`; do
echo "conversion $file encoding $SE to $DE"
iconv -f $SE -t $DE "$file" > "$file".tmp
mv -f "$file".tmp "$file"
done
@GiaoGiaoCat
Copy link
Author

需求

之前同事有一个项目给过来,由于他之前的开发环境是Windows的,文件编码都是GBK的,看起挺不爽的,不符合我们现在的规范。需要将里面的文件全部转换成UTF8的文件编码。

调用方式

~ ./batch_conversion_encoding.sh ~/sdk1 java GBK UTF8

@lqinggang
Copy link

首先谢谢,
其次,试了一下出现一个错误:
find: paths must precede expression
按网上的方法 ,将 .$FT 改成 ".$FT" 后是可以用
另:使用file -i filename 之后文件编码还是没变,不知道什么原因

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment