ubuntu 查看文件编码并进行批量编码修改

ubuntu下的安装命令:

sudo apt-get install enca


enca查看文件

enca -L zh_CN file 查看文件编码

enca -L zh_CN -x UTF-8 file 更改文件编码

enca -L zh_CN -x UTF-8 < file1 > file2 不想覆盖原文件


下面是一个脚本,批量更改文件编码

[plain]  view plain copy print ?
  1. #!/bin/bash  
  2. #将文件编码更改为UTF-8  
  3. #用法  
  4. #1. 将文件命名为set_encoding.sh   
  5. #2. chmod +x set_encoding.sh  
  6. #3. ./set_encoding.sh   
  7. #4. 输入目录名称  
  8. #5. 输入是否递归更改  
  9. #$1表示是否要递归修改文件编码  
  10. function change_file_encoing(){  
  11.     for file in $(ls -l|awk '{print $9}')  
  12.     do  
  13.         if [[ -d "$file" && $1 = y ]];then  
  14.            cd $file  
  15.            echo $file  
  16.            change_file_encoing $1  
  17.            cd ..  
  18.         elif [[ -f "$file" ]];then  
  19.            echo $file  
  20.            enca -L zh_CN -x UTF-8 $file  
  21.         fi;  
  22.     done;   
  23.         #ecna -L zh_CN file UTF-8  
  24. }  
  25.   
  26. read -p "please enter the dir path:" path #读取目录路径  
  27. if [ ! -x "$path" ];    #判断目录是否存在且是否具有执行权限  
  28. then   
  29.     echo "dir path not exists"  
  30. else  
  31.     read -p "please enter if you want to recursive?y/n:" recur  #是否递归  
  32. fi  
  33. if [ $recur = "y" ];  
  34. then  
  35.     cd $path  
  36.     change_file_encoing "y"     #递归修改文件编码  
  37. else  
  38.     cd $path  
  39.     change_file_encoing "n"     #非递归修改  
  40. fi  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值