Skip to content

Instantly share code, notes, and snippets.

@chuangzhu
Last active June 20, 2019 00:26
Show Gist options
  • Save chuangzhu/7599171821d18b1cf5b3e010b5ccc0bc to your computer and use it in GitHub Desktop.
Save chuangzhu/7599171821d18b1cf5b3e010b5ccc0bc to your computer and use it in GitHub Desktop.
`tree` util implemented in pure bash
#!/bin/bash
# all files (include dirs) under a dir
_allunder() {
local f; for f in $1/* $1/.*; do
case $f in
"$1/*" | "$1/.*" | "$1/." | "$1/..") ;;
*) printf "$f " ;;
esac
done
}
fo() {
local files=(`_allunder $2`)
local i; for i in ${!files[@]}; do
local f=${files[i]}
local j; for j in $1; do
if (( $j )); then
printf '│ '
else
printf ' '
fi
done
if [[ $i = $[${#files[@]}-1] ]]; then
printf '└── '
else
printf '├── '
fi
echo ${f##*/} # $i ${#files[@]}
[[ -d $f ]] && {
if [[ $i = $[${#files[@]}-1] ]]; then
fo "$1 0" $f
else
fo "$1 1" $f
fi
}
done
}
[[ $@ ]] || set -- .
for d in $@; do
echo $d
fo '' $d
done
@chuangzhu
Copy link
Author

asciicast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment