shell脚本递归遍历目录及子目录的例子分享
2014-04-09 00:00
811 查看
用shell写的递归遍历目录的脚本,脚本实现递归遍历指定目录,打印目录下的文件名。
实例1:
实例2:递归读取目录及其子目录
这样给test.sh加上执行权限即可执行
到此即可通过传递参数来读取目录文件了。
实例3:
实例1:
#!/bin/sh function scandir() { local cur_dir parent_dir workdir workdir=$1 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} cd .. else echo ${cur_dir}/${dirlist} fi done } if test -d $1 then scandir $1 elif test -f $1 then echo "you input a file but not a directory,pls reinput and try again" exit 1 else echo "the Directory isn't exist which you input,pls input a new one!!" exit 1 fi
实例2:递归读取目录及其子目录
#! /bin/bash function read_dir(){ for file in `ls $1` do if [ -d $1"/"$file ] //注意此处之间一定要加上空格,否则会报错 then read_dir $1"/"$file else echo $1"/"$file fi done } #测试目录 test read_dir test
这样给test.sh加上执行权限即可执行
chmod +x test.sh sh test.sh
到此即可通过传递参数来读取目录文件了。
实例3:
递归实现各个子目录孙目录...... #!/bin/bash #modify.func doit() //处理当前目录下的非目录文件,忽略目录文件 { oldname=`ls | grep "$1$"` for name in $oldname do if [ -d "$name" ] then : else basename=`echo $name | awk -F "." '{print $1}'` newname="$basename$2" echo -e "$PWD/$name\t\t$newname" mv $name $newname count=`expr ${count} + 1` fi done return 0 } do_recursive() //从当前目录开始,递归处理各目录 { doit $1 $2 for filename in `ls` do if [ -d "$filename" ] then cd $filename do_recursive $1 $2 cd .. fi done return 0 } modify() //处理当前目录,并报告结果,这个相当于主函数,也可以直接调用do_recursive { PARAMS=2 if [ $# -ne $PARAMS ] then echo "usage: mv_to .suf1 .suf2" return 1 fi count=0 do_recursive $1 $2 echo "complete! $count files have been modified." return 0 }
相关文章推荐
- shell脚本递归遍历目录及子目录的例子分享
- 分享一个遍历当前文件夹下所以子目录,并在子目录中执行其他操作的shell脚本
- shell 脚本实现的遍历目录修改子目录及其文件权限
- 递归遍历目录的shell脚本
- Shell脚本递归遍历目录所有文件
- Shell脚本实现的一个简易Web服务器例子分享
- Makefile 递归遍历目录(含子目录) 编译动态库
- shell递归遍历目录
- shell编程之如何遍历进入当前目录下的所有子目录
- 遍历当前目录下的所有子目录例子(自动生成SQl语句工具)
- 分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写
- 把当前目录和子目录下的java文件,从GBK转换成UTF-8字符集的shell脚本
- Shell脚本遍历目录并批量修改文件编码
- shell脚本 for循环实现文件和目录遍历
- shell 递归遍历目录下的所有文件并统一改名的方法
- shell脚本遍历子目录
- 分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里
- python小例子之1 -- 递归遍历目录
- shell脚本 遍历所有目录
- Shell脚本遍历目录并批量修改文件编码