summaryrefslogtreecommitdiffhomepage
path: root/tests/scripts/treewalk.sh
blob: fe38ba3b7f4453597e921c959416cf38540db3ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Integration: walk a directory tree with recursion and globbing.
mkdir -p t/a/b t/c t/.hid
touch t/f1 t/a/f2 t/a/b/f3 t/a/b/f4 t/c/f5 t/.hid/f6 't/sp ace'
walk() { # dir depth
    for e in "$1"/* "$1"/.[!.]*; do
        [ -e "$e" ] || continue
        printf '%*s%s%s\n' $(($2 * 2)) '' "${e##*/}" "$( [ -d "$e" ] && echo /)"
        [ -d "$e" ] && walk "$e" $(($2 + 1))
    done
}
walk t 0
count() {
    n=0
    for e in "$1"/*; do
        if [ -d "$e" ]; then
            count "$e"
            n=$((n + $?))
        elif [ -e "$e" ]; then
            n=$((n + 1))
        fi
    done
    return $n
}
count t; echo "files (no dot dirs): $?"