-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsd
executable file
·60 lines (53 loc) · 1.35 KB
/
lsd
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
#!/bin/bash
# INSTALL: symlink or copy this file to /usr/local/bin
#
# List only directories in a folder
#
# Usage:
# $ lsd [-a] [-c regex] [-l] [file_path]
#
# Positional Arguments:
# * file_path: Folder to list directories for, defaults to current directory
#
# Options:
# * [-a]: Include hidden directories
# * [-c regex]: Print file count matching the regex next to each directory
# * [-l]: Formats output like `ls -l` command
# Parese CLI optional and positional arguments -------------------------------
while getopts "alc:" opt; do
case $opt in
a)
all=true;;
c)
count=true
regex=\"$OPTARG\";;
l)
details=true;;
\?)
exit 1;;
:)
exit 1;;
esac
done
all=${all:-false}
count=${count:-false}
details=${details:-false}
shift $((OPTIND - 1))
file_path=${1:-$(pwd)}
if [[ "$count" = true && "$details" = true ]]; then
echo "Options -c and -l are mutally exclusive"
exit 1
fi
# Generate output ------------------------------------------------------------
cmd="ls $file_path -l"
if [[ "$all" = true ]]; then
cmd="$cmd -a"
fi
cmd="$cmd | grep ^d"
if [[ "$details" = false ]]; then
cmd="$cmd | awk '{ print \$9 }'"
fi
if [[ "$count" = true ]]; then
cmd="paste <($cmd | xargs -I {} count -d {} $regex) <($cmd) | sort -nk1 -r"
fi
eval $cmd