Linux 遍历输出指定目录下的所有子目录

创建脚本

# vim list_alldir.sh
#!/bin/sh

read -p "请输入需要遍历的目录:" folder

list_alldir(){
    for file2 in `ls -a $1`
        do
            if [ x"$file2" != x"." -a x"$file2" != x".." ];then
            if [ -d "$1/$file2" ];then
            echo "$1/$file2"
            list_alldir "$1/$file2"
        fi
        fi
    done
}
IFS=$'\n' # 遍历带空格的文件夹
list_alldir ${folder}

脚本授权

# chmod +x list_alldir.sh

执行脚本

# ./list_alldir.sh

image.png

结果保存至文件

# ./list_alldir.sh > folder.txt && cat folder.txt

image.png


标题:Linux 遍历输出指定目录下的所有子目录
作者:Mune
地址:https://cnxiaobai.com/articles/2021/05/12/1620831863380.html

    评论
    1 评论
    2023-11-09 21:43 回复»

    why not tree

avatar

取消