OSX更改文件编码(iconv)递归

19
我知道在OSX下可以使用以下命令将单个文件编码转换为UTF-8: iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx 我需要对一堆特定扩展名的文件进行转换,所以我想将文件编码从ISO-8859-1转换为UTF-8,适用于/mydisk/myfolder中所有*.ext文件。
希望有人能够提供如何执行此操作的语法。
谢谢。
ekke
7个回答

25

Adam的评论给了我解决问题的方法,但这是唯一一个我让它工作的语法:

find /mydisk/myfolder -name \*.xxx -type f | \
    (while read file; do
        iconv -f ISO-8859-1 -t UTF-8 "$file" > "${file%.xxx}-utf8.xxx";
    done);
-i ... -o ... 不起作用,但是 > 可以。
谢谢您。
ekke

1
覆盖已找到的文件 #!/bin/bash find ./tmp -type f | \ (while read file; do iconv -f windows-1251 -t UTF-8 "$file" -o "$file"; done); - temni
2
谢谢。它帮了我很多。我做了这样的事情:#!/bin/bash find ./src -type f | \ (while read file ; do if [[ "$file" != *.DS_Store* ]]; then if [[ "$file" != *-utf8* ]]; then iconv -f CP1251 -t UTF-8 "$file" > "$file-utf8"; rm $file; mv "$file-utf8" "$file"; fi fi done); - Roman Truba
谢谢。这是对我有效的代码(1行): find ./ -name AppLocalization.resx -type f | (while read file; do iconv -f UTF-16LE -t UTF-8 AppLocalization.resx > "AppLocalization-UTF-8.resx"; done); - jfmg

3
如果您的shell是bash,可以按照以下操作:
for files in /mydisk/myfolder/*.xxx
do
  iconv -f ISO-8859-1 -t UTF-8 "$files" "${files%.xxx}-utf8.xxx"
done

2

以下是在Mac 10.10上测试过的示例。 通过名称查找文件,转换编码,然后替换原始文件。效果完美。 感谢Roman Truba的示例,将下面的完整代码复制到您的Shell脚本中。

   #!/bin/bash
        find ./ -name *.java -type f | \
        (while read file;
            do if [[ "$file" != *.DS_Store* ]]; then
            if [[ "$file" != *-utf8* ]]; then
                iconv -f ISO-8859-1 -t UTF-8 "$file" > "$file-utf8";
                rm $file;
                echo mv "$file-utf8" "$file";
                mv "$file-utf8" "$file";
            fi
        fi 
        done);

我不得不改成:find ./ -name "*.java" -type f, 否则它将无法递归地运行。 - carlosvini

1
我扩展了Albert.Qing的脚本:

  • autodetect the current file encoding
  • added a command parameter to do a dry/exec-run
  • added a parameter for the directory and filename pattern

    #!/bin/bash
    command=${1-"usage"}
    searchPattern=${2-"*.java"}
    searchDirectory=${3-"."}
    if [[ "$command" == "usage" ]]; then
        echo "convert-file-to-utf8.sh [usage|dry|exec] [searchPattern=$searchPattern] [searchDirectory=$searchDirectory]"
        exit
    fi
    find $searchDirectory -type f -name "$searchPattern" | \
    (while read file;
        do if [[ "$file" != *.DS_Store* ]]; then
        if [[ "$file" != *-utf8* ]]; then
            currentEncoding="$(file --brief --mime-encoding $file)"
            if [[ "$currentEncoding" != "utf-8" ]]; then
               echo "command:$command / iconv -f $currentEncoding -t UTF-8 $file"
               if [[ "$command" == "exec" ]]; then
                 iconv -f $currentEncoding -t UTF-8 "$file" > "$file-utf8";
                 rm $file;
                 echo mv "$file-utf8" "$file";
                 mv "$file-utf8" "$file";
              fi
            fi
        fi
    fi
    done);
    

在MacOS X 10.12.6 / Sierra上进行测试。


我该如何更改这个脚本,以便它可以接受文件名中带有空格的文件?谢谢。Frank - franc

1

试试这个...已经测试并且可行:

第一步(ICONV): find /var/www/ -name *.php -type f | (while read file; do iconv -f ISO-8859-2 -t UTF-8 "$file" > "${file%.php}.phpnew"; done)

第二步(重写-MV): find /var/www/ -name "*.phpnew" -type f | (while read file; do mv $file echo $file | sed 's/\(.*\.\)phpnew/\1php/' ; done)

这只是我的研究结论 :)

希望能有所帮助 Jakub Rulec


0

如果你想要递归地执行它,你可以使用find(1)函数:

find /mydisk/myfolder -name \*.xxx -type f | \
    (while read file; do
        iconv -f ISO-8859-1 -t UTF-8 -i "$file" -o "${file%.xxx}-utf8.xxx
    done)

请注意,我使用了| while read而不是find的-exec选项(或者管道符号后接xargs)来处理文件,因为我们需要对文件名进行一些操作,即去掉.xxx扩展名(使用${file%.xxx}),并添加-utf8.xxx

0
你可以用任何脚本语言编写一个脚本,遍历/mydisk/myfolder下的每个文件,使用正则表达式[.(.*)$] 检查扩展名,如果是"ext",则从系统调用中运行以下命令(或等效命令)。
"iconv -f ISO-8859-1 -t UTF-8 " + file.getName() + ">" + file.getName() + "-utf8.xxx"
在Python中,这只需要几行代码,但我把它留给读者去了解具体的目录迭代和正则表达式的细节。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接