diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-12-17 17:21:20 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-12-17 17:21:20 +1300 |
commit | c0ddb0f83f6a8c5bb77880323261b7d6fe06e4cc (patch) | |
tree | 17c35dea69d1bf7f21297ea542fce33d754db7ff /bin | |
parent | Add an issue (diff) | |
download | dotfiles-c0ddb0f83f6a8c5bb77880323261b7d6fe06e4cc.tar.gz dotfiles-c0ddb0f83f6a8c5bb77880323261b7d6fe06e4cc.zip |
Resolve an issue with find(1) compatibility
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/cf | 32 | ||||
-rwxr-xr-x | bin/cfr | 34 |
2 files changed, 21 insertions, 45 deletions
@@ -5,32 +5,20 @@ # 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 . -prune -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 'cf: %s: not a directory\n' "$dir" + # If not, set the error flag and continue ex=1 - continue fi - - # Count the files - count=$( - find "$fdir" -path "$fdir"'/*' -prune -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 @@ -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 |