tree.sh

Graphically displays the folder structure of a path.

This shell script that is…

  1. …a working implementation of MS-DOS TREE -command for *nix shell (bash)
  2. …simple and easy to understand example for writing recursive functions with Bourne Shell
  3. …overally a simple but good piece of standard Bourne Shell code (scroll down to view source)


This program is free software licensed under GNU General Public License.

Download: tree-1.0b.tar.gz - 8.3 KiB

Usage:

tree.sh [-f] [-a] [directory]
-f Display filenames in each directory

-a Not implemented yet. Use graphic characters instead of standard ascii for drawing lines.
Propably too much hassle and increase in lines of code to implement method that outputs line graphics in various different terminals (one way could be shell implementation of curses) for small app like this.
Example of script output:

[robsku@salamanteri ~]$ ./scripts/tree.sh ~/programming/
/home/robsku/programming/
  +-- bash
  |   +-- tree
  |   |   +-- current
  |   +-- usblock
  +-- perl
      +-- ffind
      +-- irssi
      +-- oggenc-ui
      |   +-- oggenc-ui-0.1.0b
      |   +-- oggenc-ui-0.2.0
      +-- regexptest
      +-- sitemove
          +-- sitemove-0.1

Source code listing:

#!/bin/sh
##############################################################
# tree.sh, implementation of DOS tree for Bourne Again Shell #
# December 17, 2008                                          #
VERSION="1.0b"                                               #
##############################################################
# Copyright (C) 2008 Jani Saksa <robsku@fiveam.org>
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#  
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#  

# Whetever to list files under directories or not. Default is
# to list subdirectories only (cmd line parameter -f is used to
# list files too).
all=0

# ————————————————————– #
# listDirs (depth, directory, pre_echo)                          #
# List subdirectories, and if $all is true files under directory #
# Parameters:                                                    #
# $depth: Un-used value set to how deep we are                   #
# $directory: Directory to list content from                     #
# $pre_echo: Line graphics to echo in front of content           #
# ————————————————————– #
function listDirs () { # depth, directory, pre_echo
local old_dir
local depth
local thisdir
local pre_echo
local dircount
local dircount2
local i
  let depth=$1+1
  thisdir=$2
  dircount=0
  pre_echo=$3
  # Save current directory and cd to next
  old_dir=`pwd`
  cd "$thisdir"
  # Count subdirectories 1st - number of subdirs, if any,
  # is needed for correctly drawing line-graphics.
  for i in *; do
    foo=`echo "$thisdir/$i"`
    if [ -d "$foo" ]; then
      let dircount=$dircount+1
    fi
  done
  # List files (if requested)
  if [ $all -eq 1 ]; then
    for i in *; do
      if [ -f "$i" ]; then
        if [ $dircount -eq 0 ]; then
          echo -n "$pre_echo    "
        else
          echo -n "$pre_echo  | "
        fi
        echo "$i"
      fi
    done
  fi
  dircount2=0
  for i in *; do
    foo=`echo "$thisdir/$i"`
    if [ -d "$foo" ]
    then
      let dircount2=$dircount2+1
      if [ $dircount2 -lt $dircount ]; then
        foo2=`echo "$pre_echo  | "`
      else
        foo2=`echo "$pre_echo    "`
      fi
      echo -n "$pre_echo  +– "
      echo $i
      # Call recursively with $pre_echo set accordingly
      # to directory beeing last one or not.
      listDirs $depth "$foo" "$foo2"
    fi
  done
  cd "$old_dir"
}

USAGE="Usage: tree.sh [-f] [path]"

# ————————————————————– #
# showHelp ()                                                    #
# Show help/usage text                                           #
# ————————————————————– #
function showHelp () {
  echo "$USAGE
Graphically displays the folder structure of a path.

  path           Where to begin displaying directory tree from,
                 default is current directory.

  -f             Displays also files in subdirectories
  -h, –help     Show this help
  -v, –version  Show version, license & contant information.

Report bugs to <robsku@fiveam.org>"
}

# ————————————————————– #
# showHelp ()                                                    #
# Show version, license & contact information                    #
# ————————————————————– #
function showVersion () {
echo "tree.sh, v.$VERSION
Copyright (C) 2008 Jani Saksa <robsku@fiveam.org>
This is free software.  You may redistribute copies of it under the terms of
the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.
There is NO WARRANTY, to the extent permitted by law.

Available at <http://salamanteri.homelinux.net/wordpress/software/>"
}

# TODO: Write a proper command line parser

# No parameters? Use current directory as base
if [ -z "$1" ]; then
  listDirs 0 . ; exit 0
fi

while [ "$1" ]
do
  # -h for help, -V for version&license
  if [ "$1" = "-h" -o "$1" = "–help" ]; then # -o is plain sh compatible ||
    showHelp ; exit 0
  elif [ "$1" = "-v" -o "$1" = "–version" ]; then
    showVersion ; exit 0
  fi

  if [ "$1" = "-f" ]; then
    all=1
  else
    if [ -d "$1" ]; then
      echo $1
      listDirs 0 "$1"
    else
      echo "Parameter ‘$1′ neither directory nor recognized option."
      echo "Try ‘tree.sh -h’ for help."
      exit 1
    fi
  fi
  shift
done
exit 0
 

admin

Author is a 29 years old linux zealot and hacker from Finland.

You can leave a response, or trackback from your own site.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>