aboutsummaryrefslogtreecommitdiff
path: root/bin/cfr
blob: 2126238d57cd2a5e681753d368642e399456e7df (plain) (blame)
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
#!/bin/sh
# Count all descendants of given directories; don't follow symlinks

# Iterate over remaining non-option arguments (directories); default to current
# directory if none given
for dir in "${@:-.}" ; do

    # Strip a trailing slash
    dir=${dir%/}

    # If the path is not absolute or already pre-dotted, tack a ./ to
    # the front so that find(1) doesn't choke; otherwise juse use it
    # as-is
    case $dir in
        /*|./*) fdir=$dir ;;
        *) fdir=./$dir ;;
    esac

    # Warn if a non-directory was given, flag errors, but continue
    if ! [ -d "$dir" ] ; then
        printf >&2 'cfr: %s: Not a directory\n' "$dir"
        ex=1
        continue
    fi

    # Count the files recursively
    count=$(
        find "$fdir" -path "$fdir"'/*' -exec printf %.sx {} + |
        wc -c
    )

    # Print the count and the dirname
    printf '%u\t%s\n' "$count" "$dir"
done

# Exit non-zero if a non-directory was given as an argument
exit "${ex:-0}"