Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 268
user243655 Avatar asked Aug 11 '10 05:08

user243655


People also ask

How do I see the tree structure of a directory in Linux?

You need to use command called tree. It will list contents of directories in a tree-like format. It is a recursive directory listing program that produces a depth indented listing of files. When directory arguments are given, tree lists all the files and/or directories found in the given directories each in turn.

How do I display a directory tree?

In the Windows command prompt you can use "tree /F" to view a tree of the current folder and all descending files & folders. In File Explorer under Windows 8.1: Select folder.


2 Answers

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.

like image 153
crafty Avatar answered Sep 17 '22 19:09

crafty


You can use this one:

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

It will show a graphical representation of the current sub-directories without files in a few seconds, e.g. in /var/cache/:

   .    |-apache2    |---mod_cache_disk    |-apparmor    |-apt    |---archives    |-----partial    |-apt-xapian-index    |---index.1    |-dbconfig-common    |---backups    |-debconf 

Source

like image 25
Soufiane Hassou Avatar answered Sep 17 '22 19:09

Soufiane Hassou