shell 对一个目录树的遍历及遍历文件处理
2011-11-22 15:42
495 查看
做毕业设计,在将windows上的jsp网页移植到linux环境中时,发现一个个的转换编码及修改默认编码类型太慢,写此脚本进行尝试文件遍历~
好久不写,手生了。
[/code]
本文出自 “Mo” 博客,请务必保留此出处http://molinux.blog.51cto.com/2536040/721965
好久不写,手生了。
#!/bin/bash # # SPATH="/root/chengji/WebRoot" DPATH="/web" # 函数开始部分 CYCLING(){ filelist=`ls -1 $SPATH` for filename in $filelist ; do 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 该句为前期便利效果测试 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 如果遇到目录进行自我调用。。。实现深层遍历 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 } # 命令开始部分 cd $SPATH CYCLING echo "All Done." 当然,上面的代码由于使用了函数循环调用,显的很臃肿。下面来一种简单的方法,find一下: [code] #/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 not 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."
[/code]
本文出自 “Mo” 博客,请务必保留此出处http://molinux.blog.51cto.com/2536040/721965
相关文章推荐
- [置顶] Java遍历一个目录下的所有文件
- 如何使用perl遍历一个目录下的所有文件
- 递归遍历目录树,并处理文件
- Shell脚本遍历目录并批量修改文件编码
- 递归遍历一个文件目录下的所有文件
- 一个遍历指定目录下所有文件的例子
- Linux通过shell实现递归列目录及对文件的处理
- 遍历一个目录(含子目录)下所有文件
- 一个在DOS下处理中文和长名字目录和文件的程序(原创)
- shell 脚本实现的遍历目录修改子目录及其文件权限
- Office 2003 VBA 遍历目录下的Excel批量拷贝Sheet 组成一个大的Excel文件
- shell脚本遍历目录文件
- shell遍历hdfs上的文件目录
- PHP处理Linux中生成文件目录及读取内容遍历应用
- 用递归遍历一个目录下的所有文件
- shell命令:给当前目录里一个文件压缩一份不包含.svn文件的zip包
- [Shell和批处理]_[枚举指定目录下的文件并处理]
- 批处理 把文本中指定的文件从一个目录复制到另外一个目录
- shell脚本 for循环实现文件和目录遍历
- 自己写的处理文件的类,删除目录,复制目录,遍历目录,获取文件属性,点评一下啊