Shell scripts traverse directories and bulk modify file encoding _linux Shell

Source: Internet
Author: User
Tags mkdir

When porting JSP pages on Windows to a Linux environment, it is too slow to find a transform encoding and modifying the default encoding type, write this script to attempt file traversal ~

Long time no write, Rusty.

Copy Code code as follows:

#!/bin/bash
#
#
Spath= "/root/chengji/webroot"
Dpath= "/web"
# function Start part
Cycling () {
filelist= ' Ls-1 $SPATH '
for filename in $filelist; Todo
If [f $filename]; Then
echo Filename: $filename
/usr/bin/iconv-f gbk-t UTF-8 $SPATH/$filename-o $DPATH/$filename
#cp-PV $SPATH/$filename $DPATH/$filename This sentence is a preliminary convenience effect test
Sed-i-E ' s/gb2312/utf-8/g '-e ' s/gb2312/utf-8/g ' $DPATH/$filename
elif [D $filename]; Then
Dpath= $DPATH/$filename
MKDIR-PV $DPATH
CD $filename
Spath= ' pwd '
# Next for Recurse If you encounter a directory for self invocation ... Implement deep traversal
Cycling
# Next Usag:basename dirname
Dpath= ' DirName $DPATH '
Spath= ' DirName $SPATH '
CD $SPATH
Else
echo "File $SPATH/$filename is not a common file. Please check. "
Fi
Done
}
# Command Start section
CD $SPATH
Cycling
echo "All done."

Of course, the above code because of the use of the function loop call, significantly bloated. Here's an easy way to find:

Copy Code code as follows:

#/bin/bash
#Auth: Mo
#Desc:
#
Spath= "/root/chengji"
Dir=webroot
Dpath= "/web"
Find ${dir}-type d-exec MKDIR-PV ${dpath}/{} \;
Find ${dir}-type f-exec iconv-f gbk-t UTF-8 {}-o ${dpath/{} \;
echo "The file Next listed is isn't a common file or directory, please check."
Find ${dir}! -type f-a! -type d-ecec ls-l {} \;
Find $DPATH-type f-exec sed-i-E ' s/gb2312/utf-8/g '-e ' s/gb2312/utf-8/g ' {} \;
echo ""
echo "All done."

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.