14

How can you pretty print a directory structure with all subdirectories to the deepest level in the linux terminal(ubuntu)? This is an example of such a pretty print output for a mysite folder:

mysite
|-- manage.py
|-- mysite
    |-- __init__.py
    |-- settings.py
    |-- urls.py
    |-- wsgi.py
|-- polls
    |-- __init__.py
    |-- models.py
    |-- tests.py
    |-- views.py
    |-- static
        |-- css
            |-- styles.css
|-- templates
    |-- polls
        |-- index.html
16

You can use the tree command. You might need to install it if it is not already installed.

| improve this answer | |
1

A shell only version, usage: dirtree

DIR=${1:-`pwd`}
SPACING=${2:-|} 
cd $DIR 
for x in * ; do
  [ -d $DIR/$x ] && echo "$SPACING\`-{"$x && $0 $DIR/$x "$SPACING "
done
| improve this answer | |
  • Hard to follow this, and hard to use. It doesn’t want to run for me. (I’m not on ubuntu though, but this looks portable enough.) What do you mean, usage: dirtree? – Alan H. Aug 6 '15 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.