如何在Bash中递归列出子目录,而无需使用“查找"目录.或"ls"命令? [英] How to recursively list subdirectories in Bash without using "find" or "ls" commands?
本文介绍了如何在Bash中递归列出子目录,而无需使用“查找"目录.或"ls"命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道您可以将find
命令用于此简单作业,但是我得到一个分配,以不使用find
或ls
来完成该作业.我该怎么办?
I know you can use the find
command for this simple job, but I got an assignment not to use find
or ls
and do the job. How can I do that?
推荐答案
您可以只使用shell
you can do it with just the shell
#!/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中递归列出子目录,而无需使用“查找"目录.或"ls"命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文