---
title: "在Linux下将整个目录的文件转换字符编码的脚本"
author: "codingstandards"
date: "2012-06-18T20:47:15Z"
tags: []
categories: ["操作系统"]
slug: "linux"
draft: false
lastmod: "2012-06-18T20:47:38Z"
id: 1563470
url: "https://codingstandards.iteye.com/blog/1563470"
---

源文件编码：gbk

源文件名称模式：\*.ftl.gbk

目标文件编码：utf8

目标文件名称模式：\*.ftl

 

脚本：to\_utf8.sh

```
#!/bin/sh

for srcfile in *.gbk;
do
        dstfile=${srcfile/.gbk/}
        if [ "$srcfile" -nt "$dstfile" ]; then
                echo convert $srcfile to $dstfile ...
                iconv -f gbk -t utf8 $srcfile >$dstfile
        fi
done
```

 

执行示例：

 

当有新的文件或新的修改时

\[root@s170 ws1\_hndy\_wapmoban]# ./to\_utf8.sh  
convert sitemap.ftl.gbk to sitemap.ftl ...  
\[root@s170 ws1\_hndy\_wapmoban]#

 

当没有修改时

\[root@s170 ws1\_hndy\_wapmoban]# ./to\_utf8.sh  
\[root@s170 ws1\_hndy\_wapmoban]#

 

本文链接：[http://codingstandards.iteye.com/blog/1563470](/blog/1563470)