linux下递归读取文件
2016-04-26 23:07
701 查看
转http://www.jb51.net/article/48832.htm
用shell写的递归遍历目录的脚本,脚本实现递归遍历指定目录,打印目录下的文件名。
实例1:
复制代码 代码如下:
local cur_dir parent_dir workdir
workdir=1cd{workdir}
if [ workdir=“/”]thencurdir=””elsecurdir=(pwd)
fi
}
if test -d 1thenscandir1
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:递归读取目录及其子目录
复制代码 代码如下:
这样给test.sh加上执行权限即可执行
复制代码 代码如下:
chmod +x test.sh
sh test.sh
到此即可通过传递参数来读取目录文件了。
实例3:
复制代码 代码如下:
递归实现各个子目录孙目录……
{
oldname=
for name in oldnamedoif[−d“name” ]
then :
else
basename=
newname=”basename2”
echo -e “PWD/name\t\tnewname”mvname $newname
count=
fi
done
return 0
}
do_recursive() //从当前目录开始,递归处理各目录
{
doit 12
for filename in
do
if [ -d “filename”]thencdfilename
do_recursive 12
cd ..
fi
done
return 0
}
modify() //处理当前目录,并报告结果,这个相当于主函数,也可以直接调用do_recursive
{
PARAMS=2
if [ # -nePARAMS ]
then
echo “usage: mv_to .suf1 .suf2”
return 1
fi
count=0
do_recursive 12
echo “complete! $count files have been modified.”
return 0
}
用shell写的递归遍历目录的脚本,脚本实现递归遍历指定目录,打印目录下的文件名。
实例1:
复制代码 代码如下:
!/bin/sh
function scandir() {local cur_dir parent_dir workdir
workdir=1cd{workdir}
if [ workdir=“/”]thencurdir=””elsecurdir=(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 1thenscandir1
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 oldnamedoif[−d“name” ]
then :
else
basename=
echo $name | awk -F "." '{print $1}'
newname=”basename2”
echo -e “PWD/name\t\tnewname”mvname $newname
count=
expr ${count} + 1
fi
done
return 0
}
do_recursive() //从当前目录开始,递归处理各目录
{
doit 12
for filename in
ls
do
if [ -d “filename”]thencdfilename
do_recursive 12
cd ..
fi
done
return 0
}
modify() //处理当前目录,并报告结果,这个相当于主函数,也可以直接调用do_recursive
{
PARAMS=2
if [ # -nePARAMS ]
then
echo “usage: mv_to .suf1 .suf2”
return 1
fi
count=0
do_recursive 12
echo “complete! $count files have been modified.”
return 0
}
相关文章推荐
- Linux的远程连接
- Linux内核学习期末总结(网课)
- Linux查看操作系统位数
- linux学习
- Linux交叉开发环境搭建 —— 效率之源
- 每天一个linux命令:du
- 每天一个linux命令:df
- linux内核期中总结
- 亚马逊AWS免费套餐EC2安装centos连接登录并创建root
- Linux 0.11几个重要的切换
- Linux设备之字符驱动设备(一)
- Linux下mplayer源代码安装方法
- linux基础命令(5)——rm命令
- linux系统下批量解压方法汇总
- 关于一些实验班问题的解答以及下个学期之前小组的任务
- Linux chmod命令 修改文件权限被禁止(not permitted)的解决办法
- linux lnmp安装
- Linux每天学习一个命令之find命令三
- Linux的chattr与lsattr命令详解
- Linux进程管理-调度-通信-