Skip to content

Instantly share code, notes, and snippets.

@ushis
Created March 31, 2012 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ushis/2269339 to your computer and use it in GitHub Desktop.
Save ushis/2269339 to your computer and use it in GitHub Desktop.
tree... in bash
#!/bin/sh
_tree() {
IFS=$'\x0a'
local dir len i d
dir=($(ls "$1" --group-directories-first 2>/dev/null))
len=${#dir[@]}
for (( i = 0; i < len; i++ )); do
if (( i == len - 1 )); then
printf '%s└' "$2"
else
printf '%s├' "$2"
fi
d="${1}/${dir[i]}"
if [ -d "$d" ]; then
printf '── \e[1;34m%s\e[0m\n' "${dir[i]}"
if (( i == len -1 )); then
_tree "$d" "${2} "
else
_tree "$d" "${2}│ "
fi
else
printf '── %s\n' "${dir[i]}"
fi
done
unset IFS
}
if [ $# -lt 1 ]; then
base="$PWD"
else
base="$1"
fi
printf '\e[1;34m%s\e[0m\n' "$base"
_tree "$base"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment