Wednesday, October 24, 2012

[COMMAND LINE] Display structure of directory hierarchy

Let's say you're working on the terminal and want to display current's directory hierarchy in a tree structure, like this:

$ .kde/share/apps/krita/


I found the command below on the web and its author even provided it as a shell script to use it easily.

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

Install system wide

Open a terminal window and run:

dst="/usr/local/bin/" && sudo wget -O $dst && sudo chmod +x $dst

Install for current user

This will install the script for your user only and assumes you have a bin directory on your home directory and it has been added to $PATH. Open a terminal window and run:

dst="$HOME/bin/" && wget -O $dst && chmod +x $dst


Unix Tree / Linux Tree - Display Structure of Directory Hierarchy


  1. Why not just using the `tree` command? It does the same and has plenty of more options.

  2. Up until now the script has all the functionality I need. Besides tree is not pre-installed and since I have a place for scripts outside the operating system partition, it'll be available if I choose to format.

    But still, tree is a option to take in consideration. Thank you.

  3. "sudo wget http:// [...]" is an extremely bad idea. You don't just download something from obscured URLs as root, make it executeable and put it into /usr/local/bin/. It's a recipe for disaster. This kind of carelessness will compromize your system sooner or later. Users who are inclined to do this shouldn't have sudo access at all.

    1. Thank you for your concern, but the script provided here just runs the command quote on the main text of the article. And anyone who distrust this can check the contents of the file manually.