forked from ISART-GP2018/bash_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.sh
35 lines (30 loc) · 818 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
if [ "$#" == 1 ]; then
CURRENT_PATH=$(pwd)
cd $1
fi
oldIFS=$IFS
IFS=$'\t\n'
listdir()
{
local currentPath=$1 prefix=$2
local -a currentDir=($(ls $1))
local -i lastIndex=$((${#currentDir[*]} - 1)) index
for ((index=0; index<lastIndex; index++))
do
printf "%s├─%s\n" $prefix ${currentDir[$index]}
if [ -d "$currentPath/${currentDir[$index]}" ]; then
listdir "$currentPath/${currentDir[$index]}" $prefix"│ "
fi
done
if [ $lastIndex -ge 0 ]; then
printf "%s└─%s\n" $prefix ${currentDir[$lastIndex]}
if [ -d "$currentPath/${currentDir[$index]}" ]; then
listdir "$currentPath/${currentDir[$index]}" $prefix" "
fi
fi
}
listdir $PWD ' '
IFS=$oldIFS
if [ "$#" == 1 ]; then
cd $CURRENT_PATH
fi