Created
March 13, 2009 18:55
-
-
Save abachman/78702 to your computer and use it in GitHub Desktop.
A bash script for listing directory trees (with color)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# This script produces a complete tree structure for the directory | |
# in which it is running. | |
# | |
# FROM | |
# http://www.sun.com/bigadmin/scripts/submittedScripts/lstree.sh.txt | |
# | |
# Modifications by Adam Bachman | |
# | |
# TODO | |
# * allow more specific file highlighting. | |
line=" " | |
DIRCOLOR="\033[1;34m" # blue | |
FILECOLOR="\033[0;32m" # green | |
RST="\033[0;m" # original | |
rst() { | |
echo -en "$RST" | |
echo | |
} | |
rdir() { | |
echo -en "$DIRCOLOR$1/" | |
} | |
dirStru() | |
{ | |
incl=' ' | |
dname="$1" | |
cd "$dname" >/dev/null | |
if [ "$?" -eq 0 ]; then | |
for k in `ls`; do | |
if [[ -d "$k" ]]; then | |
echo -en " $line$(rdir $k)" | |
rst | |
line=$incl$line | |
dirStru "$k" | |
cd .. | |
#remove smallest prefix pattern(.....) | |
line=${line# } | |
elif [[ -f "$k" ]]; then | |
echo -en " $line$FILECOLOR$k" | |
rst | |
fi | |
done | |
fi | |
} | |
# main | |
#Tree sub-directory structure under $1 directory" | |
if [ $# -ne 1 ]; then | |
echo "Usage:$(basename $0) <directory-name>" | |
exit 1 | |
fi | |
basedir=`pwd` | |
rootls=`ls $1` | |
rootdir=$(basename $1) | |
echo -en "$(rdir $rootdir)" | |
rst | |
for i in $rootls; do | |
reldir="$rootdir/$i" | |
if [[ -d "$reldir" ]]; then | |
echo -en " $(rdir $i)" | |
rst | |
dirStru "$reldir" | |
elif [[ -f "$reldir" ]]; then | |
echo -en " $FILECOLOR$i" | |
rst | |
fi | |
cd $basedir | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment