Sarah
Sarah

Reputation: 25

How can I go about printing a directory tree in a bash script?

I want the output to be something like

|-------File1 

|-------File2

|-------Folder

       |-------somefile

       |-------Makefile

       |-------Directory

                |-------hi.txt
|-------hello.c

I am writing a bash script to obtain this kind of output. I'm having trouble on how to go about this. I want to do this without using the tree command.

Upvotes: 1

Views: 42

Answers (1)

heemayl
heemayl

Reputation: 42107

Don't reinvent the wheel, this is exactly what tree command does.

Check man tree to get details.


All popular distros should have tree (installed or) available in their repositories; so if not installed already you can install it using usual manner.

For example, in Debian (and derivatives):

sudo apt-get install tree

In RPM based distros:

sudo yum install tree

Upvotes: 4

Related Questions