编写shell脚本,使用iconv批量改变文件编码

在开发中,我们经常需要对N多文件编码进行更改,iconv只能对单文件的进行更改,怎么办呢?我们写一个shell脚本来解决这个问题。

用法示例:

cd ~/workspace/XXXProject

~/iconv_shell.sh ./ *java

好了,直接上代码~~

#!/bin/bash

if [ "$#" != "2" ]; then

    echo "Usage: `basename $0` dir filter"

    exit

fi

dir=$1

filter=$2

echo $1

for file in `find $dir -name "$2"`; do

    echo "$file"

    iconv -f gbk -t utf8 -o $file $file

done

这里还有些修改的余地,比如源和目标编码应该作为参数,我暂时没有这个需求,懒得写了。

One thought on “编写shell脚本,使用iconv批量改变文件编码

Leave a Reply to 林博仁 Cancel reply

Your email address will not be published. Required fields are marked *