How to recursively list subdirectories in Bash without using “find” or “ls” commands?

前端 未结 10 2140
情歌与酒
情歌与酒 2020-12-01 17:22

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 th

10条回答
  •  忘掉有多难
    2020-12-01 17:42

    Since it is for bash, it is a surprise that this hasn't been already said:
    (globstar valid from bash 4.0+)

    shopt -s globstar nullglob dotglob
    echo **/*/
    

    That's all.
    The trailing slash / is there to select only dirs.

    Option globstar activates the ** (search recursivelly). Option nullglob removes an * when it matches no file/dir. Option dotglob includes files that start wit a dot (hidden files).

提交回复
热议问题