diff options
-rwxr-xr-x | bin/cf | 17 | ||||
-rwxr-xr-x | bin/cfr | 28 |
2 files changed, 39 insertions, 6 deletions
@@ -1,22 +1,31 @@ #!/bin/sh # Count entries in a given set of directories -self=cf # Iterate over remaining non-option arguments (directories); default to current # directory if none given for dir in "${@:-.}" ; do + # Strip a trailing slash + dir=${dir%/} + + # If the path is not absolute or already pre-dotted, tack a ./ to + # the front so that find(1) doesn't choke; otherwise juse use it + # as-is + case $dir in + /*|./*) fdir=$dir ;; + *) fdir=./$dir ;; + esac + # Warn if a non-directory was given, flag errors, but continue if ! [ -d "$dir" ] ; then - printf >&2 '%s: %s: not a directory\n' \ - "$self" "$dir" + printf >&2 'cf: %s: not a directory\n' "$dir" ex=1 continue fi # Count the files count=$( - find "$dir" -path "$dir"'/*' -prune -exec printf %.sx {} + | + find "$fdir" -path "$fdir"'/*' -prune -exec printf %.sx {} + | wc -c ) @@ -1,13 +1,37 @@ #!/bin/sh # Count all descendants of given directories; don't follow symlinks + +# Iterate over remaining non-option arguments (directories); default to current +# directory if none given for dir in "${@:-.}" ; do + + # Strip a trailing slash + dir=${dir%/} + + # If the path is not absolute or already pre-dotted, tack a ./ to + # the front so that find(1) doesn't choke; otherwise juse use it + # as-is + case $dir in + /*|./*) fdir=$dir ;; + *) fdir=./$dir ;; + esac + + # Warn if a non-directory was given, flag errors, but continue if ! [ -d "$dir" ] ; then printf >&2 'cfr: %s: Not a directory\n' "$dir" ex=1 continue fi - count=$(find "$dir" -exec printf %.sx {} + | wc -c) - count=$((count - 1)) + + # Count the files recursively + count=$( + find "$fdir" -path "$fdir"'/*' -exec printf %.sx {} + | + wc -c + ) + + # Print the count and the dirname printf '%u\t%s\n' "$count" "$dir" done + +# Exit non-zero if a non-directory was given as an argument exit "${ex:-0}" |