서버 이전을 하다보면 시대에 맞게 EUC-KR에서 UTF-8로 변환을 해 주어야 할 때가 있다. 이 경우에 여러가지 방법이 있지만 스크립트를 이용하는 것이 편하다.

다음의 스크립트는 내가 발로 짠 거라 그리 좋지는 않지만 쓸만 할 것이다.

# filename: euckr2utf8.sh
# usage: ./euckr2utf8.sh source target
# made by Heesung SHIN (ensual@gmail.com)
#
#!/bin/bash
recurse () {
    rm -rf $2
    echo make the directory $2
    mkdir $2
    for file in $(ls $1)         do
            name=$1/$file
            echo -n $2/$file | iconv -fcp949 -tutf8 -o temp # change the encoding of the name of file
            for newname in $(cat temp)
                do
                if [ -d $name ]; then
                    recurse $name $newname
                else
                    echo convert $file to $newname
                    iconv -fcp949 -tutf8 $name -o $newname # change the encoding of the content of file
                    if [ $? -ne 0 ]; then
                        echo "-->" copy the $file to $newname
                        cp $name $newname
                    fi
                fi
            done ;
        done ;
}
recurse $1 $2
rm temp
# end of file

이 스크립트의 기능은 다음과 같이 요약할 수 있다.

  1. target 폴더를 새로 만든다. (source 폴더와 이름이 겹치지 않도록 주의!)
  2. 파일 이름의 인코딩을 변환한다.
  3. 파일 내용의 인코딩을 변환한다.
  4. JPG, HWP 와 같은 바이너리 코드는 변환 중에 에러가 발생하므로, 에러가 발생한 것들은 인코딩 변환없이 파일 이름만 바꾸어 복사를 한다.

+ Recent posts