没有合适的资源?快使用搜索试试~ 我知道了~
首页shell脚本递归遍历目录及子目录的例子分享
资源详情
资源评论
资源推荐

shell脚本递归遍历目录及子目录的例子分享脚本递归遍历目录及子目录的例子分享
主要介绍了shell脚本递归遍历目录及子目录的例子,需要的朋友可以参考下
用shell写的递归遍历目录的脚本,脚本实现递归遍历指定目录,打印目录下的文件名。
实例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加上执行权限即可执行

















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0