aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/cf.bash
blob: 587c44b35a193e91cc537c869b08e8b9155d058e (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
# Count files
cf() {
    local dirname

    # Specify directory to check
    dirname=${1:-$PWD}

    # Error conditions
    if [[ ! -e $dirname ]] ; then
        printf 'bash: %s: %s does not exist\n' \
            "$FUNCNAME" "$dirname" >&2
        return 1
    elif [[ ! -d $dirname ]] ; then
        printf 'bash: %s: %s is not a directory\n' \
            "$FUNCNAME" "$dirname" >&2
        return 1
    elif [[ ! -r $dirname ]] ; then
        printf 'bash: %s: %s is not readable\n' \
            "$FUNCNAME" "$dirname" >&2
        return 1
    fi

    # Count files and print; use a subshell so options are unaffected
    (
        shopt -s dotglob nullglob
        declare -a files=("$dirname"/*)
        printf '%u\t%s\n' "${#files[@]}" "$dirname"
    )
}
complete -A directory cf