user3153765
user3153765

Reputation: 373

How to change encoding of files in batch

I have tons of files encoded in Japanese (Shift JIS) and I have to change the encoding of them to UTF-8

With VSCode, or some other editors such as Sublime, Emacs, I can open those files with encoding Shift JIS and then save them with encoding UTF-8.

How to change encoding of all files under a folder, including subfolders?

Upvotes: 0

Views: 1958

Answers (1)

user3153765
user3153765

Reputation: 373

Here is the shell script:

function encode() 
{
    iconv -f shift_jis -t utf-8 "$1" > test
    # iconv -f iso8859-15 -t utf8 "$1" > test;
    cat test > "$1";
}

function walk()  
{  
  for file in `ls $1`  
  do  
    local path=$1"/"$file  
    if [ -d $path ]  
     then  
      echo "DIR $path"  
      walk $path  
    else  
      echo "FILE $path"  
      encode $path
    fi      
  done  
}  

if [ $# -ne 1 ]  
then  
  echo "USAGE: $0 TOP_DIR"  
else  
  walk $1  
fi  

Upvotes: 1

Related Questions