如何递归列出在bash子目录,而不使用find或LS命令? [英] How to recursively list subdirectories in bash without using find or ls commands?

查看:147
本文介绍了如何递归列出在bash子目录,而不使用find或LS命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以使用find命令为这个简单的工作。但是,我得到了一个分配不使用查找或ls和做的工作。请帮助....

I know you can use find command for this simple job. But I got an assignment not to use find or ls and do the job. Please help....

推荐答案

您可以只用贝壳做

#!/bin/bash
recurse() {
 for i in "$1"/*;do
    if [ -d "$i" ];then
        echo "dir: $i"
        recurse "$i"
    elif [ -f "$i" ]; then
        echo "file: $i"
    fi
 done
}

recurse /path

或者如果你有bash的4.0

OR if you have bash 4.0

#!/bin/bash
shopt -s globstar
for file in /path/**
do
    echo $file
done

这篇关于如何递归列出在bash子目录,而不使用find或LS命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆