aboutsummaryrefslogblamecommitdiff
path: root/bash/bashrc.d/cf.bash
blob: 587c44b35a193e91cc537c869b08e8b9155d058e (plain) (tree)
1
2
3
4
5
6
7
8
9

             
                 
 
                                
                      

                      
                                 
                                                
                                      
                
                                   
                                                    
                                      
                
                                   
                                                 
                                      
                

      

                                                                     
                                 
                                       
                                                   
     
 
                        
# 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