linuxcommand-line

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


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
folder2
   folder3

Solution

  • 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.