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

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

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

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