diff options
-rwxr-xr-x | bin/cf | 47 | ||||
-rw-r--r-- | man/man1/cf.1 | 7 |
2 files changed, 25 insertions, 29 deletions
@@ -1,27 +1,20 @@ -#!/usr/bin/env bash - -# Count files -self=cf - -# Specify directory to check, defaults to current dir -dirname=${1:-"$PWD"} - -# Error conditions -if [[ ! -e $dirname ]] ; then - printf '%s: %s does not exist\n' \ - "$self" "$dirname" >&2 - exit 1 -elif [[ ! -d $dirname ]] ; then - printf '%s: %s is not a directory\n' \ - "$self" "$dirname" >&2 - exit 1 -elif [[ ! -r $dirname ]] ; then - printf '%s: %s is not readable\n' \ - "$self" "$dirname" >&2 - exit 1 -fi - -# Count files and print; use dotglob and nullglob so we get an accurate count -shopt -s dotglob nullglob -declare -a files=("$dirname"/*) -printf '%u\t%s\n' "${#files[@]}" "$dirname" +#!/bin/sh +# Count entries in a given set of directories +ex=0 +for dir in "${@:-.}" ; do + if ! [ -d "$dir" ] ; then + printf >&2 'cf: %s: not a directory\n' "$dir" + ex=1 + continue + fi + c=0 + for ent in "$dir"/* "$dir"/.* ; do + [ -e "$ent" ] || continue + case $ent in + */.|*/..) ;; + *) c=$((c+1)) ;; + esac + done + printf '%u\t%s\n' "$c" "$dir" +done +exit "$ex" diff --git a/man/man1/cf.1 b/man/man1/cf.1 index 4c30c9d8..0bedb19f 100644 --- a/man/man1/cf.1 +++ b/man/man1/cf.1 @@ -6,9 +6,12 @@ .B cf .br .B cf /path/to/dir +.br +.B cf dir1 dir2 .SH DESCRIPTION .B cf -counts all the entries in a given directory using Bash glob expansion and -prints the count. It defaults to the current directory. +counts all the entries in the given directories using glob +expansion and prints the count. It defaults to the current +directory. .SH AUTHOR Tom Ryder <tom@sanctum.geek.nz> |