How to change encoding in many files?

前端 未结 4 927
甜味超标
甜味超标 2020-12-17 03:10

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?

相关标签:
4条回答
  • 2020-12-17 03:51

    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:

    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 {} \;
    
    0 讨论(0)
  • 2020-12-17 03:54

    read about enconv.
    If you need to convert to your current terminal encoding you can do it like that:

    find . -exec enconv -L czech {}\;
    

    Or exactly what you wanted:

    find . -exec enconv -L czech -x utf8 {}\;
    
    0 讨论(0)
  • 2020-12-17 03:55

    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 ls-tree master -r --name-only | grep htm | xargs -n1 -I{} bash -c 'recode "$(file -b --mime-encoding {})..utf-8" {}'

    0 讨论(0)
  • 2020-12-17 04:06

    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).

    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

    0 讨论(0)
提交回复
热议问题