-
Notifications
You must be signed in to change notification settings - Fork 0
/
lr
executable file
·91 lines (81 loc) · 2.23 KB
/
lr
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
######################################################################
# Author: [email protected]
# Date: Sun Jan 12 17:04:30 2014
# File: lr
#
# Usage: lr [-w width] [-d depth] [dir...]
# Description: Recursively list contents of `dir' as a tree structure
######################################################################
readonly DFT_WIDTH=4
readonly DFT_LEVEL=6
readonly DFT_DIR="."
width="${DFT_WIDTH}"
max_level="${DFT_LEVEL}"
dashes=""
blanks=""
# $1 is the level of current directory
# $2 is the target directory
# $3 is the leading characters
function trace_dir()
{
if (( $1 > "${max_level}" )); then
echo "$3|${dashes}..."
return
fi
local total=0
for tryfile in $(ls $2); do
let total=total+1;
done
local count=0
for tryfile in $(ls $2); do
echo "$3|${dashes}${tryfile}"
let count=count+1
local dir="$2/${tryfile}"
if [[ -d "$dir" ]]; then
if ((count == total)); then
# This is the last entry
trace_dir $(($1+1)) "${dir}" "$3 ${blanks}"
else
trace_dir $(($1+1)) "${dir}" "$3|${blanks}"
echo "$3|${blanks}"
fi
fi
done
}
function print_usage()
{
echo "Usage: $0 [-w width] [-d depth] [dir...]" >&2
echo "Recursively list contents of \`dir' (default is ${DFT_DIR}) as a tree structure" >&2
echo "-w: output width of each level, default is ${DFT_WIDTH}" >&2
echo "-d: maximum depth of subdirectory, default is ${DFT_LEVEL}" >&2
}
while getopts ":w:d:" opt; do
case $opt in
w ) width="${OPTARG}" ;;
d ) max_level="${OPTARG}" ;;
\?) print_usage; exit 1 ;;
esac
done
shift $(($OPTIND-1))
for (( i = 1; i < "${width}"; ++i )); do
dashes="${dashes}-"
blanks="${blanks} "
done
if [[ -n "$@" ]]; then
let count=0
for dir in "$@"; do
if ((count++ >= 1)); then
echo -e "\n--------------------------------------------------------\n"
fi
if [[ ! -d "${dir}" ]]; then
echo "${dir} is not a directory!" >&2
else
echo "${dir%/}/"
trace_dir 1 "${dir}" ""
fi
done
else
echo "./"
trace_dir 1 "." ""
fi