Skip to content

Instantly share code, notes, and snippets.

@mina86
Created December 5, 2010 11:59
Show Gist options
  • Save mina86/729036 to your computer and use it in GitHub Desktop.
Save mina86/729036 to your computer and use it in GitHub Desktop.
tree.sh
#!/bin/sh
# See http://damienix.jogger.pl/2010/12/01/skrypt-bashowy-wyswietlajacy-drzewko-struktury-katalogow/
clr_dir='\33[1;34m' # Blue
clr_fil='\33[0;33m' # Yellow
clr_rst='\33[0m' # Text Reset
chain() {
# $1 $2 - no change
# $3 - prefix for this dir
# $4 - prefix for nexts
if [ -d "$1" ]; then
COLOR=$clr_dir
else
COLOR=$clr_fil
fi
printf "$2$3$COLOR %s$clr_rst\\n" "${1##*/}"
if [ -d "$1" ]; then
showBranch "$1" "$2$4"
fi
}
# Prints branch and recursive its childs
showBranch() {
# $1 - current dir
# $2 - current prefix
for i in "$1/"* "$1/".*; do
case $i in */.|*/..)
continue
esac
if ! [ -e "$i" ] && ! [ -L "$i" ]; then
break;
fi
set -- "$i" "$2" "$3"
[ -z "$3" ] || chain "$3" "$2" '|--' '| '
set -- '' "$2" "$1"
done
[ -z "$3" ] || chain "$3" "$2" '`--' ' '
}
dir=${1:-$PWD}
if ! [ -d "$dir" ]; then
echo "$dir: not a directory" >&2
exit 1
else
printf "$clr_dir%s$clr_rst\\n" "$dir"
showBranch "$dir" ' '
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment