linux下将该目录下及子目录下所有的文件转成UTF-8的格式

时间:2023-01-21 12:26:33


1.先安装recode和uchardet命令

sudo apt-get -y install recode uchardet > /dev/null


2.创建脚本文件convert.sh如下:


#!/bin/bash
find "$1" -type f | while read FFN # 'dir' should be changed...
do
encoding=$(uchardet "$FFN")
echo "$FFN: $encoding"
enc=`echo $encoding | sed 's#^x-mac-#mac#'`
set +x
recode $enc..UTF-8 "$FFN"
done


3.运行命令

chmod +x convert.sh
./convert.sh dir_you_want_to_UTF-8