List all directories recursively in a tree format

前端 未结 5 1940
春和景丽
春和景丽 2020-12-28 09:47

I want to simulate a tree command using Shell Script that displays all the directories recursively in this format:

.
|-- Lorem
|-- Lorem
|-- Lor         


        
5条回答
  •  醉话见心
    2020-12-28 09:53

    Modifying sputnick's answer to get closer to your original format (which I prefer):

    find ./ -type d -print | sed -e 's;[^/]*/; /;g;s;/ ;    ;g;s;^ /$;.;;s; /;|-- ;g'
    

    The only difference now is the last line doesn't start with a backtick:

    .
    |-- Lorem
    |-- Lorem
    |-- Lorem
        |-- Lorem
        |-- Lorem
    |-- Lorem
    |-- Lorem
    

    with awk

    find . -type d -print 2>/dev/null | awk '!/\.$/ {for (i=1;i

提交回复
热议问题