[Linux] Bulk conversion file encoding for the entire directory is UTF-8;
#!/bin/bash - #===============================================================================## FILE: conv.sh# # USAGE: ./conv.sh # # DESCRIPTION: A script that supports recursive conversion of the entire directory GB2312 to UTF-8;# # options: ---# Operating system;# bugs: for the Requirements: linux kernel Currently does not support the incoming parameters contain spaces;# notes: input support three formats, customized with your heart;# AUTHOR: linkscue (scue), [email protected]# created: 2013 March 06 22 52 minutes 31 seconds HKT# COPYRIGHT: Copyright (c) 2013, linkscue# Revision: 0.1# organization: ---#===============================================================================set -o nounset # Treat unset variables as an error#------------------------------------------------------------------------- ------# Check for error #-------------------------------------------------------------------------------if [[ $# == 0 ]] ; then echo "" echo "program version: 0.1" echo "Implementation function: Batch conversion GB2312 character encoding to UTF-8;" echo "How to: $ (basename $0) < suffix name > < file 1> < file 2> < directory 1> < Catalogue 2>  ... " echo "Action tip:" echo " 1. Suffix name does not need '. ' This symbol; echo 2. when no suffix is appended to the input parameter, it is removed from the incoming file and prompts for further action; " echo "" exit 1fi#------------------------------------ -------------------------------------------# Incoming parameter case 1# the first parameter passed is the suffix name;# Judgment is based on the first parameter is not a file; #-------------------------------------------------------------------------------if [ [ ! -f $1 ]] ; then suffix=$1fitarget=${@:2:$#}for n in ${target[@]};d o # determine if it is a file; if [[ -f $n ]] ; then iconv -f gb2312 -t UTF-8 $n -o $n 2> /dev/null fi #判断是否是一个目录 if [[ -d $n ]] ; then find "$n" -name "*. $suffix" | while Read line ; do iconv -f gb2312 -t UTF-8 "$line" -o "$line" 2> /dev/null done fidone#--------------------------- ----------------------------------------------------# Incoming parameter case 2# the first parameter passed in is a file;# If there is no directory in the incoming parameter, the file will be transcoded directly;# if there is a directory in the incoming parameter, it will ask whether to use the first parameter suffix as the search directory condition; #------------------------------------- ------------------------------------------if [[ -f $1 ]] ; then # determine if there is a directory in the incoming parameter; for n in [email protected] ; do if [[ -d "$n" ]]; then has_dir=true fi done # transcoding incoming files directly when there are no directories in the incoming parameters; if [[ $ has_dir != "true" ]]; then for n in [email protected] ; do iconv -f gb2312 -t UTF-8 "$n" -o "$n" 2> /dev/null done else # when an incoming parameter contains a directory, the suffix of the first passed-in parameter is used; suffix=${1# #*.} if [[ $suffix != "" ]]; then read -p ">> The suffix named $suffix is found for the first incoming parameter.Do you want to use it as a condition for the search catalog? [y/n] " if [[ $REPLY == "Y" ]]; then for n in [email protected] ; do if [[ -f "$n" ]]; then iconv -f gb2312 -t utf-8 "$n" -o "$n" 2> /dev/null fi if [[ -d "$n" ]]; then find "$n" -name "*. $suffix" | while read line ; do iconv -f gb2312 -t utf-8 "$line" -o "$line" 2> /dev/null done fi done fi fi fifi#-------------------------------------------------------------------------------# incoming parameter case 3# The first parameter passed in is the directory;# at this time consider that the user may convert the entire directory, but forget to lose the conversion file suffix name;# The user will be prompted to enter a suffix name;# can enter multiple suffix names, For example, the Android program source code contains XML and Java suffix; #-------------------------------------------------------------------------------if [[ -d $1 ]]; then [email protected] # prompt user for the suffix name of the input file; read -p ">> found that the file suffix you want to convert has not been entered, enter the suffix to be transcoded [ Can enter multiple]: " suffix_3 for n in ${argvs[@]};d o # processing is a general document; if [[ -f "$n" ]]; then iconv -f gb2312 -t utf-8 "$n" -o "$n" 2> /dev/null fi # is dealing with a directory; if [[ -d "$n" ]]; then for suffix in ${suffix_3[@]} ; do find "$n" -name " *. $suffix " | while read line ; do iconv -f gb2312 - t utf-8 "$line" -o "$line" 2> /dev/null done done fi Donefi
Operating instructions:
If you save this script content to ~/bin/conv, and give permission;
Examples of Use:
1. conv < suffixes > < files 1> < files 2> < directories 1> < directories 2> < directories or files: >
Conv Java aaa.java bbb.java Android source code catalog 1/android source code Catalog 2/
2. conv < file 1> < file 2> < directory 1> < directory 2> < directory or file: > Note: The first file suffix will be used as the search criteria for a directory
Conv Aaa.java bbb.java Android source code directory 1/android source code catalog 2/
3. CONV < catalogue 1> < directory 2> < directories or files > # Note: You will be prompted for a suffix name (multiple suffix names can be entered)
Conv Android Source code directory 1/android source code directory 2/aaa.java bbb.java>> found that you have not entered the file suffix to convert, enter the suffix to be transcoded [can enter multiple]:xml java
[Linux] Bulk conversion file encoding for the entire directory is UTF-8;