Skip to content

Instantly share code, notes, and snippets.

@damienix
Forked from mina86/gist:729036
Created December 16, 2010 14:10
Show Gist options
  • Save damienix/743432 to your computer and use it in GitHub Desktop.
Save damienix/743432 to your computer and use it in GitHub Desktop.
Prints directory tree
#!/bin/sh
# Title : tree
# See : http://damienix.jogger.pl/2010/12/01/skrypt-bashowy-wyswietlajacy-drzewko-struktury-katalogow/
# Date : 2010-12-16
# Author : Damian Skrodzki <damienix1@gmail.com>
# Description : Prints directory tree
clr_dir='\033[1;34m' # Blue
clr_fil='\033[0;33m' # Yellow
clr_rst='\033[0m' # Text Reset
chain() {
# $1 $2 - no change
# $3 - prefix for this dir
# $4 - prefix for nexts
[ -d "$1" ] && COLOR=$clr_dir || COLOR=$clr_fil
printf "$2$3$COLOR %s$clr_rst\\n" "${1##*/}"
[ -d "$1" ] && showBranch "$1" "$2$4"
}
# Prints branch and recursive its childs
showBranch() {
# $1 - current dir
# $2 - current prefix
for i in "$1/"* ; do
set -- "$i" "$2" "$3"
[ -z "$3" ] || chain "$3" "$2" '|--' '| '
set -- '' "$2" "$1"
done
[ -z "$3" ] || chain "$3" "$2" '`--' ' '
}
shopt -s dotglob # show also hidden files using *
shopt -s nullglob # return empty when pattern doesn't match
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