Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 64 lines (48 sloc) 0.986 kB
61367e9 @falconindy walk: initial commit
authored
1 #!/bin/bash
2
01238a7 @falconindy walk: add comment header
authored
3 #
4 # emulates the 'tree' utility
5 #
6
dc9ed00 @falconindy walk: only set dotglob on -a
authored
7 shopt -s nullglob
b409874 @falconindy walk: add 'dirs first' option
authored
8
b102556 @falconindy walk: remove useless logic to determine walking dirs
authored
9 indent=0
b32a303 @falconindy walk: cleanup. handle whitespace more elegantly
authored
10 ind_sz=3
11 dirs_first=0
e37e9d6 @falconindy walk: add -f(iles-only) option
authored
12 files_only=0
61367e9 @falconindy walk: initial commit
authored
13
92694bb @falconindy walk: separate print functionality
authored
14 printentry() {
15 printf "%*s%s\n" "$indent" "" "$1"
16 }
17
005f73a @falconindy walk: change default behavior to dirs first
authored
18 get_pwd_contents() {
19 local dir nondir
20
21 for f in *; do
22 [[ -d $f ]] && dir+=("$f") || nondir+=("$f")
23 done
24
b32a303 @falconindy walk: cleanup. handle whitespace more elegantly
authored
25 dir_contents=("${dir[@]}" "${nondir[@]}")
005f73a @falconindy walk: change default behavior to dirs first
authored
26 }
27
61367e9 @falconindy walk: initial commit
authored
28 walk() {
b409874 @falconindy walk: add 'dirs first' option
authored
29 local dir_contents
005f73a @falconindy walk: change default behavior to dirs first
authored
30
e37e9d6 @falconindy walk: add -f(iles-only) option
authored
31 if (( !files_only )); then
32 printentry "$(printf "\e[0;34m%s\e[0m" "$1")"
33 (( indent += ind_sz ))
34 fi
35
61367e9 @falconindy walk: initial commit
authored
36 pushd "$1" &>/dev/null
005f73a @falconindy walk: change default behavior to dirs first
authored
37
b32a303 @falconindy walk: cleanup. handle whitespace more elegantly
authored
38 (( dirs_first )) && get_pwd_contents || dir_contents=(*)
a4726de @falconindy s/echo/printf/ -- and some extra juju to go with
authored
39 for entry in "${dir_contents[@]}"; do
e37e9d6 @falconindy walk: add -f(iles-only) option
authored
40 if [[ -d $entry && ! -L $entry ]]; then
41 walk "$entry"
42 else
43 printentry "$entry"
44 fi
61367e9 @falconindy walk: initial commit
authored
45 done
005f73a @falconindy walk: change default behavior to dirs first
authored
46
e37e9d6 @falconindy walk: add -f(iles-only) option
authored
47 (( !files_only )) && (( indent -= ind_sz ))
61367e9 @falconindy walk: initial commit
authored
48 popd &>/dev/null
49 }
50
e37e9d6 @falconindy walk: add -f(iles-only) option
authored
51 while getopts "afFd" opt; do
b409874 @falconindy walk: add 'dirs first' option
authored
52 case $opt in
dc9ed00 @falconindy walk: only set dotglob on -a
authored
53 a) shopt -s dotglob ;;
b32a303 @falconindy walk: cleanup. handle whitespace more elegantly
authored
54 d) dirs_first=1 ;;
e37e9d6 @falconindy walk: add -f(iles-only) option
authored
55 f) files_only=1 ;;
b409874 @falconindy walk: add 'dirs first' option
authored
56 esac
57 done
58 shift $(( OPTIND - 1 ))
61367e9 @falconindy walk: initial commit
authored
59
b102556 @falconindy walk: remove useless logic to determine walking dirs
authored
60 for basedir in "${@:-.}"; do
b409874 @falconindy walk: add 'dirs first' option
authored
61 walk "$basedir"
62 done
63
Something went wrong with that request. Please try again.