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

后端 未结 9 995
野趣味
野趣味 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:28

    You can also use the combination of find and awk commands to print the directory tree. For details, please refer to "How to print a multilevel tree directory structure using the linux find and awk combined commands"

    find . -type d | awk -F'/' '{ 
    depth=3;
    offset=2;
    str="|  ";
    path="";
    if(NF >= 2 && NF < depth + offset) {
        while(offset < NF) {
            path = path "|  ";
            offset ++;
        }
        print path "|-- "$NF;
    }}'
    
    0 讨论(0)
  • 2020-11-28 00:29

    This command works to display both folders and files.

    find . | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"
    

    Example output:

    .
     |-trace.pcap
     |-parent
     | |-chdir1
     | | |-file1.txt
     | |-chdir2
     | | |-file2.txt
     | | |-file3.sh
     |-tmp
     | |-json-c-0.11-4.el7_0.x86_64.rpm
    

    Source: Comment from @javasheriff here. Its submerged as a comment and posting it as answer helps users spot it easily.

    0 讨论(0)
  • 2020-11-28 00:29

    Adding the below function in bashrc lets you run the command without any arguments which displays the current directory structure and when run with any path as argument, will display the directory structure of that path. This avoids the need to switch to a particular directory before running the command.

    function tree() {
        find ${1:-.} | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"
    }
    

    This works in gitbash too.

    Source: Comment from @javasheriff here

    0 讨论(0)
  • 2020-11-28 00:32

    Is this what you're looking for tree? It should be in most distributions (maybe as an optional install).

    ~> tree -d /proc/self/
    /proc/self/
    |-- attr
    |-- cwd -> /proc
    |-- fd
    |   `-- 3 -> /proc/15589/fd
    |-- fdinfo
    |-- net
    |   |-- dev_snmp6
    |   |-- netfilter
    |   |-- rpc
    |   |   |-- auth.rpcsec.context
    |   |   |-- auth.rpcsec.init
    |   |   |-- auth.unix.gid
    |   |   |-- auth.unix.ip
    |   |   |-- nfs4.idtoname
    |   |   |-- nfs4.nametoid
    |   |   |-- nfsd.export
    |   |   `-- nfsd.fh
    |   `-- stat
    |-- root -> /
    `-- task
        `-- 15589
            |-- attr
            |-- cwd -> /proc
            |-- fd
            | `-- 3 -> /proc/15589/task/15589/fd
            |-- fdinfo
            `-- root -> /
    
    27 directories
    

    sample taken from maintainer's web page.

    You can add the option -L # where # is replaced by a number, to specify the max recursion depth.

    Remove -d to display also files.

    0 讨论(0)
  • 2020-11-28 00:33

    Since it was a successful comment, I am adding it as an answer:
    with files:

     find . | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/" 
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题