diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-11-25 20:49:00 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-11-25 20:49:00 +1300 |
commit | a823aa9f193e2d83fe7882363e59a5b06dba13f2 (patch) | |
tree | f3381b2a7fd13344a07f424f53145e243005334a /bin/cfr | |
parent | Less clumsy method of counting (diff) | |
download | dotfiles-a823aa9f193e2d83fe7882363e59a5b06dba13f2.tar.gz dotfiles-a823aa9f193e2d83fe7882363e59a5b06dba13f2.zip |
Centralize cf(1df)/cfr(1df)
They are almost exactly the same script now; there might be a better way
to do this
Diffstat (limited to 'bin/cfr')
-rwxr-xr-x | bin/cfr | 28 |
1 files changed, 26 insertions, 2 deletions
@@ -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}" |