Last active
June 20, 2019 00:26
-
-
Save chuangzhu/7599171821d18b1cf5b3e010b5ccc0bc to your computer and use it in GitHub Desktop.
`tree` util implemented in pure 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/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 |
Author
chuangzhu
commented
Jan 19, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment