shell 脚本 转码目录下文件

本文介绍了一个使用bash脚本实现的递归遍历源文件目录,并针对不同编码进行文件转码的功能。该脚本适用于批量处理大量文件编码不一致的问题,通过递归方式遍历目录结构,识别文件编码并执行转码操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash
#递归遍历源文件目录
function scandir(){
if [ $# -lt 3 ]
then
echo usage:$0 src sourcecode destcode ;
exit;
fi
local cur_dir workdir sourcecode destcode
workdir=$1
sourcecode=$2;
destcode=$3;
cd ${workdir}
if [ ${workdir} = "/" ]
then
cur_dir=""
else
cur_dir=$(pwd)
fi

for dirlist in $(ls ${cur_dir})
do
if test -d ${dirlist}
then
cd ${dirlist}
scandir ${cur_dir}/${dirlist} $sourcecode $destcode
cd ..
else
tfile=${cur_dir}/${dirlist}
echo $tfile
transFileCode "$sourcecode" "$destcode" "$tfile";
fi
done

}

#转码文件
function transFileCode(){
sourcecode=$1;
destcode=$2;
tfile=$3;
iconv -f $sourcecode -t $destcode $tfile > ${tfile}temp;
mv "${tfile}temp" "${tfile}"
}

dirname=$1; #源文件目录
sourcecode=$2;
destcode=$3;
echo '********************************************************************************';
scandir $dirname $sourcecode $destcode; #在这里进行递归遍历
echo '********************************************************************************';
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值