| 1 | #!/bin/bash |
| 2 | # tree.sh |
| 3 | |
| 4 | # Written by Rick Boivie. |
| 5 | # Used with permission. |
| 6 | # This is a revised and simplified version of a script |
| 7 | #+ by Jordi Sanfeliu (the original author), and patched by Ian Kjos. |
| 8 | # This script replaces the earlier version used in |
| 9 | #+ previous releases of the Advanced Bash Scripting Guide. |
| 10 | # Copyright (c) 2002, by Jordi Sanfeliu, Rick Boivie, and Ian Kjos. |
| 11 | |
| 12 | # ==> Comments added by the author of this document. |
| 13 | |
| 14 | |
| 15 | search () { |
| 16 | for dir in `echo *` |
| 17 | # ==> `echo *` lists all the files in current working directory, |
| 18 | #+ ==> without line breaks. |
| 19 | # ==> Similar effect to for dir in * |
| 20 | # ==> but "dir in `echo *`" will not handle filenames with blanks. |
| 21 | do |
| 22 | if [ -d "$dir" ] ; then # ==> If it is a directory (-d)... |
| 23 | zz=0 # ==> Temp variable, keeping track of |
| 24 | # directory level. |
| 25 | while [ $zz != $1 ] # Keep track of inner nested loop. |
| 26 | do |
| 27 | echo -n "| " # ==> Display vertical connector symbol, |
| 28 | # ==> with 2 spaces & no line feed |
| 29 | # in order to indent. |
| 30 | zz=`expr $zz + 1` # ==> Increment zz. |
| 31 | done |
| 32 | |
| 33 | if [ -L "$dir" ] ; then # ==> If directory is a symbolic link... |
| 34 | echo "+---$dir" `ls -l $dir | sed 's/^.*'$dir' //'` |
| 35 | # ==> Display horiz. connector and list directory name, but... |
| 36 | # ==> delete date/time part of long listing. |
| 37 | else |
| 38 | echo "+---$dir" # ==> Display horizontal connector symbol... |
| 39 | # ==> and print directory name. |
| 40 | numdirs=`expr $numdirs + 1` # ==> Increment directory count. |
| 41 | if cd "$dir" ; then # ==> If can move to subdirectory... |
| 42 | search `expr $1 + 1` # with recursion ;-) |
| 43 | # ==> Function calls itself. |
| 44 | cd .. |
| 45 | fi |
| 46 | fi |
| 47 | fi |
| 48 | done |
| 49 | } |
| 50 | |
| 51 | if [ $# != 0 ] ; then |
| 52 | cd $1 # Move to indicated directory. |
| 53 | #else # stay in current directory |
| 54 | fi |
| 55 | |
| 56 | echo "Initial directory = `pwd`" |
| 57 | numdirs=0 |
| 58 | |
| 59 | search 0 |
| 60 | echo "Total directories = $numdirs" |
| 61 | |
| 62 | exit 0 |