blob: cb06aa568e24c371cf2e8697e4a05aa0ef870d67 (
plain) (
tree)
|
|
# Count files
cf() {
local dir dgs ngs
local -a files
# 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
# 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"
}
|