Created
March 31, 2012 22:56
-
-
Save ushis/2269339 to your computer and use it in GitHub Desktop.
tree... in bash
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
#!/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