【问题标题】:osx change file encoding (iconv) recursiveosx 更改文件编码(iconv)递归
【发布时间】:2009-07-25 12:44:13
【问题描述】:

我知道我可以在 OSX 下使用以下方法转换单个文件编码:

iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx

我必须转换一堆具有特定扩展名的文件, 所以我想将文件编码从 ISO-8859-1 转换为 UTF-8 对于文件夹 /mydisk/myfolder 中的所有 *.ext 文件

也许有人知道如何做到这一点的语法

谢谢

咳咳

【问题讨论】:

    标签: macos shell encoding glob iconv


    【解决方案1】:

    亚当的评论向我展示了如何解决它, 但这是我让它工作的唯一语法:

    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 ... 不起作用,但是 >

    再次感谢

    咳咳

    【讨论】:

    • 用于覆盖已创建的文件#!/bin/bash find ./tmp -type f | \ (while read file; do iconv -f windows-1251 -t UTF-8 "$file" -o "$file"; done);
    • 谢谢。我对我帮助不大。我做到了#!/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);
    • 谢谢。这对我有用(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);
    【解决方案2】:

    如果你的 shell 是 bash,类似这样的

    for files in /mydisk/myfolder/*.xxx
    do
      iconv -f ISO-8859-1 -t UTF-8 "$files" "${files%.xxx}-utf8.xxx"
    done
    

    【讨论】:

      【解决方案3】:

      这是在 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 否则它不会递归地工作
      【解决方案4】:

      试试这个......它已经过测试并且可以工作:

      第一步(ICONV): 查找 /var/www/ -name *.php -type f | (读取文件时;执行 iconv -f ISO-8859-2 -t UTF-8 "$file" > "${file%.php}.phpnew"; 完成)

      第二步(REWRITE - MV): 查找 /var/www/ -name "*.phpnew" -type f | (读取文件时;执行 mv $file echo $file | sed 's/\(.*\.\)phpnew/\1php/' ;完成)

      这只是我研究的结论:)

      希望对你有帮助 雅库布法则

      【讨论】:

        【解决方案5】:

        我扩展了 Albert.Qings 脚本:

        • 自动检测当前文件编码
        • 添加了一个命令参数来执行干/执行运行
        • 为目录和文件名模式添加了一个参数

          #!/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 上测试。

        【讨论】:

        • 如何更改此脚本以使其接受文件名中带有空格的文件?谢谢。坦率的
        【解决方案6】:

        您可以使用任何脚本语言编写脚本来遍历 /mydisk/myfolder 中的每个文件,使用正则表达式 [.(.*)$] 检查扩展名,如果它是“ext”,则运行以下命令(或等效)来自系统调用。

        "iconv -f ISO-8859-1 -t UTF-8" + file.getName() + ">" + file.getName() + "-utf8.xxx"

        这只是 Python 中的几行代码,但我将其作为练习留给读者,让他们了解查找目录迭代和正则表达式的细节。

        【讨论】:

          【解决方案7】:

          如果你想递归做,可以使用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

          【讨论】:

            猜你喜欢
            • 2010-12-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-05-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多