This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Source for Tree command


You can try using the windows one at: c:/WINDOWS/system32/tree.com

Or you can roll you own as shown below which will also work on *nix machines.
enjoy.
Lynn

=============================

#!/bin/bash

# This scripts graphically displays a file directory structure
# Does NOT work with file names containing embedded spaces!
#
#  Written by Lynn R. Wilson  March 2001
#

depth=0
let MaxDepth=10000

function TreeHelper()
{
	local savethis
	savethis="$thisfile"
	local saveindent
	local printed
	printed=
	
	if [ -n "$ShowFiles" ]; then
		for file in $* ; do
			thisfile="$thisfile/$file"
			if [ -f "$thisfile" ]; then
				printed="1"
				echo -e "$indent|-- $file"		# add * at end if you want...
			fi
			thisfile="$savethis"
		done
	fi

for file in $* ; do

		if [ "$file" = "CVS" ] && [ -n "$ignoreCVS" ]; then
			continue
		fi

		thisfile="$thisfile/$file"
		if [ -d "$thisfile" ]; then
			if [ -L "$file" ]; then
				link=$( ls -al "$file" )
				link=${link##*>}		# keep everything after last '>'
				echo -e  "$indent|-- $file/ (symlink)-> $link"
			else
				echo -e  "$indent|-- $file/"
			fi
			printed="1"
			saveindent=$indent
			indent="$indent|   "
			if [ ! -L "$file" ] || [ -n "$FollowLinks" ]; then
               let depth=$depth+1
               if [ $depth -lt $MaxDepth ] ; then
				    TreeHelper $( command ls "$thisfile" )
               else
		            echo -e "$indent..."
               fi
               let depth=$depth-1
			fi
			indent=$saveindent
		fi
		thisfile=$savethis
	done

	if [ -n "$printed" ]; then
		echo -e "$indent"
	fi
}


# parse the command arguments # can also be done with ( while getopts ":ip:" opt; do... ) while true; do

	if [ "$1" = "-f" ]; then
		ShowFiles=1
		shift
		continue
	fi

	if [ "$1" = "-L" ]; then
		FollowLinks=1
		shift
		continue
	fi

	if [ "$1" = "-d" ]; then
		shift
		MaxDepth=$1
		shift
		continue
	fi

	if [ "$1" = "-C" ]; then
		ignoreCVS=1
		shift
		continue
	fi
	
	if [ "$1" = "-h" ]; then
		echo 'Usage:  tree [-f] [-L] [-C] [-d depth] [ path ]'
		echo '-f also shows files, -L follows symbolic links, -C ignores CVS'
		echo
		exit
	fi

	break
done



if [ -z "$1" ] ; then
	here=$( pwd )
else
	here=$1
fi

if [ ! -d "$here" ]; then
	echo Not a Directory! $here
	exit 1
fi

echo Tree rooted at: $here

thisfile=$here
TreeHelper $( command ls "$here" )

exit 0

# end of script





--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]