Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all directories recursively in a tree format

Tags:

shell

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

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

How can I do that?

like image 447
Zeno Rocha Avatar asked Jan 08 '13 20:01

Zeno Rocha


2 Answers

Try doing this (not exactly the same output, but very close) :

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

From http://mlsamuelson.com/content/tree-approximation-using-find-and-sed

with awk

find . -type d -print 2>/dev/null|awk '!/\.$/ {for (i=1;i<NF;i++){d=length($i);if ( d < 5  && i != 1 )d=5;printf("%"d"s","|")}print "---"$NF}'  FS='/'

See http://www.unix.com/shell-programming-scripting/50806-directory-tree.html

like image 62
Gilles Quenot Avatar answered Oct 06 '22 15:10

Gilles Quenot


You can just launch :

tree .

OR

tree $absolute/path/of/your/dir

If you want to display the hidden files.

By default tree does not print hidden files (those beginning with a dot '.'), just type:

tree -a .

This is what tree command do.

like image 24
craken Avatar answered Oct 06 '22 17:10

craken