bash 如何更改多个文件的编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9310571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change encoding in many files?
提问by Nips
I try this:
我试试这个:
find . -exec iconv -f iso8859-2 -t utf-8 {} \;
but output goes to the screen, not to the same file. How to do it?
但输出到屏幕,而不是同一个文件。怎么做?
回答by wobmene
Try this:
尝试这个:
find . -type f -print -exec iconv -f iso8859-2 -t utf-8 -o {}.converted {} \; -exec mv {}.converted {} \;
It will use temp file with '.converted' suffix (extension) and then will move it to original name, so be careful if you have files with '.converted' suffixes (I don't think you have).
它将使用带有“.converted”后缀(扩展名)的临时文件,然后将其移动到原始名称,所以如果你有带有“.converted”后缀的文件(我认为你没有)要小心。
Also this script is not safe for filenames containing spaces, so for more safety you should double-quote: "{}" instead of {} and "{}.converted" instead of {}.converted
此外,此脚本对于包含空格的文件名也不安全,因此为了更安全,您应该双引号:“{}”而不是 {} 和“{}.converted”而不是 {}.converted
回答by 2r2w
回答by Sebastien
I found this method worked well for me, especially where I had multiple file encodings and multiple file extensions.
我发现这种方法对我很有效,尤其是在我有多个文件编码和多个文件扩展名的情况下。
Create a vim script called script.vim:
创建一个名为 script.vim 的 vim 脚本:
set bomb
set fileencoding=utf-8
wq
Then run the script on the file extensions you wish to target:
然后在您希望定位的文件扩展名上运行脚本:
find . -type f \( -iname "*.html" -o -iname "*.htm" -o -iname "*.php" -o -iname "*.css" -o -iname "*.less" -o -iname "*.js" \) -exec vim -S script.vim {} \;
回答by TiCPU
No one proposed a way to automatically detect encoding and recode.
没有人提出一种自动检测编码和重新编码的方法。
Here is an example to recode to UTF-8 all HTM/HTML files from master branch of a GIT.
这是将 GIT 的 master 分支中的所有 HTM/HTML 文件重新编码为 UTF-8 的示例。
git ls-tree master -r --name-only | grep htm | xargs -n1 -I{} bash -c 'recode "$(file -b --mime-encoding {})..utf-8" {}'
git ls-tree master -r --name-only | grep htm | xargs -n1 -I{} bash -c 'recode "$(file -b --mime-encoding {})..utf-8" {}'

