Command line disk tree

... a very useful shell function or script ...

Posted by geoffm on Monday, January 1, 0001

Command line disk tree

I am writing another article on the basics of using git. It was going to be a short article however, given the subject it is turning out to be much longer than I expected. But in the process of writing it I had to frequently use a shell function I wrote years ago that displays a disk tree structure (optionally with files).

I needed a way to see the changes to the .git directory as git commands are executed against the repository. I thought I would share it with you today.

Here is a sample of its output:

$ dt -f
============================================================
             gwmlaptop2014 : /home/geoffm/tmp/work/.
 Run Date: Mon Apr 27 17:09:05 EDT 2015
============================================================
+-----.git
|     +-----branches
|     +-----config
|     +-----description
|     +-----HEAD
|     +-----hooks
|     |     +-----applypatch-msg.sample
|     |     +-----commit-msg.sample
|     |     +-----post-update.sample
|     |     +-----pre-applypatch.sample
|     |     +-----pre-commit.sample
|     |     +-----prepare-commit-msg.sample
|     |     +-----pre-push.sample
|     |     +-----pre-rebase.sample
|     |     +-----update.sample
|     +-----index
|     +-----info
|     |     +-----exclude
|     +-----objects
|     |     +-----5b
|     |     |     +-----d657f7b8fcd822d6e44202bc33e808cdd01ee7
|     |     +-----info
|     |     +-----pack
|     +-----refs
|     |     +-----heads
|     |     +-----tags
+-----t.f
============================================================
    Note: use "dt -f" to add files to the tree listing
Enjoy!

Here is the code writen as a function (which is in my ~/.bashrc script).

###########################################################
dt () # Print a crude dir-tree
###########################################################
{
  LINE="============================================"
  TYPE="-type d";
  if [ "${1}" = "-f" ]; then
    shift;
    TYPE="";
    ( cd ${1-"."} );
  else
    if [ "${2}" = "-f" ]; then
      TYPE="";
      ( cd ${1-"."} );
    fi;
  fi;
  echo "${LINE}";
  echo "             `hostname` : `pwd`/${1-.}  ";
  echo " Run Date: `date` ";
  echo "${LINE}";
  find ${1-.} $TYPE -print 2> /dev/null | sort -f | sed -e "s,^${1-.},," -e "/^$/d" -e "s,[^/]*/\([^/]*\)$,\+-----\1," -e "s,[^/]*/,|     ,g";
  echo "${LINE}";
  echo "    Note: use \"dt -f\" to add files to the tree listing";
  echo "Enjoy!"
}

Have a nice day!

-g-


comments powered by Disqus