-
Notifications
You must be signed in to change notification settings - Fork 4
/
tree.sh
executable file
·46 lines (35 loc) · 895 Bytes
/
tree.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env bash
exec 2> /dev/null
shopt -s nullglob
dir_count=0
file_count=0
traverse() {
dir_count=$(($dir_count + 1))
local directory=$1
local prefix=$2
local children=($directory/*)
local child_count=${#children[@]}
for idx in "${!children[@]}"; do
local child=${children[$idx]// /\\ }
child=${child##*/}
dash=""
local child_prefix="│ "
local pointer="├── "
if [ $idx -eq $((child_count - 1)) ]; then
pointer="└── "
child_prefix=" "
fi
if [ -f "$directory/$child" ]; then
dash="-";
fi
echo "${prefix}${pointer}${child} ${dash} $(head -1 "$directory/$child")"
[ -d "$directory/$child" ] &&
traverse "$directory/$child" "${prefix}$child_prefix" ||
file_count=$((file_count + 1))
done
}
root="."
[ "$#" -ne 0 ] && root="$1"
echo $root
traverse $root ""
shopt -u nullglob