From c0ddb0f83f6a8c5bb77880323261b7d6fe06e4cc Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 17 Dec 2016 17:21:20 +1300 Subject: Resolve an issue with find(1) compatibility --- bin/cfr | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) (limited to 'bin/cfr') diff --git a/bin/cfr b/bin/cfr index 2126238d..d49ab3d9 100755 --- a/bin/cfr +++ b/bin/cfr @@ -1,36 +1,24 @@ #!/bin/sh -# Count all descendants of given directories; don't follow symlinks +# Count entries in a given set of directories # Iterate over remaining non-option arguments (directories); default to current # directory if none given for dir in "${@:-.}" ; do - # Strip a trailing slash - dir=${dir%/} + # Attempt to count the files in a subshell + if count=$( + cd -- "$dir" || exit + find . ! -name . -exec printf %.sx {} + | + wc -c + ) ; then - # 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 + # If it worked, print the count + printf '%u\t%s\n' "$count" "$dir" + else - # Warn if a non-directory was given, flag errors, but continue - if ! [ -d "$dir" ] ; then - printf >&2 'cfr: %s: Not a directory\n' "$dir" + # If not, set the error flag and continue ex=1 - continue fi - - # 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 -- cgit v1.2.3