aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/cf.bash
blob: 17c84ade691516052cd48483fbc9a212cc530d7e (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 dir dgs ngs
    local -a files

    # Specify directory to check
    dir=${1:-$PWD}
    if [[ ! -d $dir ]]; then
        printf 'bash: cf: %s is not a directory\n' \
            "$dir" >&2
        return 1
    fi

    # Record current state of dotglob and nullglob
    shopt -pq dotglob && dgs=1
    shopt -pq nullglob && ngs=1

    # Retrieve the files array
    shopt -s dotglob nullglob
    files=("$dir"/*)

    # Reset our options
    ((dgs)) || shopt -u dotglob
    ((ngs)) || shopt -u nullglob

    # Print result
    printf '%d\t%s\n' \
        "${#files[@]}" "$dir"
}