aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-11-25 19:59:33 +1300
committerTom Ryder <tom@sanctum.geek.nz>2016-11-25 19:59:33 +1300
commit3ec2a4181b016f7c56784db355ce1ed403547c58 (patch)
tree3d05be87f7bad9cc2c96034f71615caa67180b80
parentSimplify cf(1df) and cfr(1df) (diff)
downloaddotfiles-3ec2a4181b016f7c56784db355ce1ed403547c58.tar.gz
dotfiles-3ec2a4181b016f7c56784db355ce1ed403547c58.zip
Simplify cf(1df) with a find(1) command
This should also work for very large numbers of files
-rwxr-xr-xbin/cf18
-rw-r--r--man/man1/cf.1df8
2 files changed, 8 insertions, 18 deletions
diff --git a/bin/cf b/bin/cf
index 2076ddc2..362e4eba 100755
--- a/bin/cf
+++ b/bin/cf
@@ -14,20 +14,14 @@ for dir in "${@:-.}" ; do
continue
fi
- # Add files matching glob, shift them off if unexpanded (first and only
- # entry doesn't exist)
- set -- "$dir"/*
- [ -e "$1" ] || shift
-
- # Add dot files, shift off the "." and ".." entries (sh(1) implementations
- # seem to vary on whether they include these)
- set -- "$dir"/.* "$@"
- [ -e "$1" ] || shift
- [ "$1" = "$dir"/. ] && shift
- [ "$1" = "$dir"/.. ] && shift
+ # Count the files
+ count=$(
+ find "$dir" ! -path "$dir" -prune -exec printf %.sx {} + |
+ wc -c
+ )
# Print the count and the dirname
- printf '%u\t%s\n' "$#" "$dir"
+ printf '%u\t%s\n' "$count" "$dir"
done
# Exit non-zero if a non-directory was given as an argument
diff --git a/man/man1/cf.1df b/man/man1/cf.1df
index 8434265b..f9d09e8e 100644
--- a/man/man1/cf.1df
+++ b/man/man1/cf.1df
@@ -12,12 +12,8 @@
dir1 dir2
.SH DESCRIPTION
.B cf
-counts all the entries in the given directories using glob expansion and prints
-the count. It defaults to the current directory.
-.P
-It uses globs to do the counting, so it will give you unexpected results if
-you're counting a directory that has more than ARG_MAX entries in it. You'll
-have to resort to find(1) loops in that case.
+counts all the entries in the given directories using find(1) and prints the
+count. It defaults to the current directory.
.SH SEE ALSO
cfr(1df)
.SH AUTHOR