Generate tree structure of any file system, graphically
Bugs - Size info unformatted
- Some combinations of options produce unwanted results
#!/bin/ksh
getsize ()
{
du -s $x | awk '{printf("( %-10d %-8.3fMB )",$1,($1*512/(1024*1024)))}'
return
}
chkroot ()
{
# list of possible directories needing
# root permissions
if [ $1 = "/" ] || [ $1 = "/usr" ]
then
x=`id`
if ( echo $x | grep root >/dev/null 2>&1 )
then
echo 1 # output to stdout
else
echo 0 # output to stdout
fi
else
echo 1
fi
}
DTREE="./DT" # scratch file name to hold 'find' output
GRPH="N" # VTxxx line glyphs disabled
GETSIZE="N" # Display size info disabled
echo "$0 : ver 1.0 (c) 1993, Krishna Prasad ">/dev/tty
echo "$0 -h for help ">/dev/tty
while getopts hlrsg:f: ch
do
sv=$ch
case $ch in
f)
err=`chkroot $OPTARG`
if [ $err -ne 1 ]
then
echo "U R not the root" >/dev/tty
echo "Querying $OPTARG require root permissions" >/dev/tty
exit
fi
filesys=$OPTARG
rm -f $DTREE 2>/dev/null # remove the scratch file
;;
s) GETSIZE="Y"
;;
g)
filesys=$OPTARG
err=`chkroot $OPTARG`
if [ $err -ne 1 ]
then
echo "U R not the root" >/dev/tty
echo "Querying $OPTARG require root permissions" >/dev/tty
exit
fi
rm $DTREE 2>/dev/null
;;
l)
GRPH="Y"
;;
*) echo "Usage $0 [-hlfgs] [directory name]" >/dev/tty
echo "-h : Show this help" >/dev/tty
echo "-g : Generate, scratch directory list in $DTREE" >/dev/tty
echo "-s : Build tree with size info " >/dev/tty
echo "-f : Name an optional directory to start building the tree" >/dev/tty
echo "-l : Use VTxxx line graphic characters" >/dev/tty
echo "default : Build tree from available scratch file $DTREE" >/dev/tty
exit
;;
esac
done
if [ ! -f $DTREE ] ; then
echo "Generating scratch file........" >/dev/tty
fs=${filesys:="."} # no -f option and no DTREE
(find $fs -type d -print > $DTREE) 2>/dev/null
if [ ! -s $DTREE ]
then
echo "Scratch file empty, cannot proceed"
rm $DTREE
exit
fi
if [ x{$sv} = x"g" ]
then
echo "scratch file generated in $DTREE"
exit
fi
fi
echo "Creating directory tree........" >/dev/tty
2>&1 cat $DTREE | \
while read x
do
if [ $GETSIZE = "Y" ]
then
siz=`getsize` # udf to get the size of the directory using 'du'
fi
dir="`dirname $x`" # get the directory name
bas="`basename $x`" # get the filename after stripping path
if [ $dir="$pdir" ] ; then
LN=`tput smacs`
LNOFF=`tput rmacs`
if [ $GRPH = "Y" ] ;then
# pdir="`echo $dir|sed -e 's@/@${LN}x${LNOFF}@g' -e 's@[^${LN}x${LNOFF}]@ @g'"
pdir="`echo $dir|sed -e 's@/@x@g' -e 's@[^x]@ @g'"
else
pdir="`echo $dir|sed -e 's@/@|@g' -e 's@[^|]@ @g'"
fi
else
pdir=$dir
fi
if [ $GRPH = "Y" ]
then
echo "$pdir${LN}t${LNOFF}${LN}q${LNOFF}$bas $siz" # VTxxx line glyphs
else
echo "$pdir|-$bas $siz"
fi
# substitute '||' with '|' for the starting dirname
done | \
if [ $GRPH = "Y" ]
then
sed -e 's/${LN}xt${LNOFF}/${LN}x${LNOFF}/' -e 's@|-/@/@'
else
sed -e 's/||/|/' -e 's@|-/@/@'
fi
# End of source dtree