Linux command to print directory structure in the form of a tree

后端 未结 9 1043
野趣味
野趣味 2020-11-28 00:05

Is there any linux command that I can call from a Bash script that will print the directory structure in the form of a tree, e.g.,

folder1
   a.txt
   b.txt
         


        
9条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 00:40

    I'm prettifying the output of @Hassou's answer with:

    ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//──/g' -e 's/─/├/' -e '$s/├/└/'
    

    This is much like the output of tree now:

    .
    ├─pkcs11
    ├─pki
    ├───ca-trust
    ├─────extracted
    ├───────java
    ├───────openssl
    ├───────pem
    ├─────source
    ├───────anchors
    ├─profile.d
    └─ssh
    

    You can also make an alias of it:

    alias ltree=$'ls -R | grep ":$" | sed -e \'s/:$//\' -e \'s/[^-][^\/]*\//──/g\' -e \'s/─/├/\' -e \'$s/├/└/\''
    

    BTW, tree is not available in some environment, like MinGW. So the alternate is helpful.

提交回复
热议问题