问题描述
我在我的 OSX 上有一个 PHP 项目,它采用 latin1 编码.现在我需要将文件转换为 UTF8.我不是一个 shell 编码员,我尝试了从互联网上找到的一些东西:
I'm having this one PHP project on my OSX which is in latin1 -encoding. Now I need to convert files to UTF8. I'm not much a shell coder and I tried something I found from internet:
mkdir new
for a in `ls -R *`; do iconv -f iso-8859-1 -t utf-8 <"$a" >new/"$a" ; done
但这不会创建目录结构,并且在运行时会给我带来大量错误.谁能想出简洁的解决方案?
But that does not create the directory structure and it gives me heck load of errors when run. Can anyone come up with neat solution?
推荐答案
你不应该像那样使用 ls
并且 for
循环也不合适.此外,目标目录应该在源目录之外.
You shouldn't use ls
like that and a for
loop is not appropriate either. Also, the destination directory should be outside the source directory.
mkdir /path/to/destination
find . -type f -exec iconv -f iso-8859-1 -t utf-8 "{}" -o /path/to/destination/"{}" ;
不需要循环.-type f
选项包括文件和排除目录.
No need for a loop. The -type f
option includes files and excludes directories.
iconv
的 OS X 版本没有 -o
选项.试试这个:
The OS X version of iconv
doesn't have the -o
option. Try this:
find . -type f -exec bash -c 'iconv -f iso-8859-1 -t utf-8 "{}" > /path/to/destination/"{}"' ;
这篇关于使用 iconv 将 latin-1 文件批量转换为 utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!