mathew7k5b
mathew7k5b

Reputation: 237

Linux command to print a tree like directory structure

I'm trying to write a script which will print directories, subdirectories and their files in a tree structure, but without using tree command.

Example:

DIR: A 
     f1
     f2 
     DIR: B
          f3 
DIR: C
     file1
     file21

I have tried multiple of solutions, but I always ran into the problem that I could not distinguish between a directory or a file, therefore I could not apply the right formatting. The "DIR: " prefix makes it complicated. Is there something very obvious that I'm missing?

Upvotes: 2

Views: 11957

Answers (2)

Nɪsʜᴀɴᴛʜ ॐ
Nɪsʜᴀɴᴛʜ ॐ

Reputation: 2914

You may try variant of these command also

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

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

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

find -type d

tree -l

Upvotes: 4

choroba
choroba

Reputation: 242423

Use the the -d test:

if [ -d "$path" ] ; then
    echo DIR
fi

Upvotes: 2

Related Questions