diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-07-30 02:29:44 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-07-30 02:29:44 +1200 |
commit | bcaaf3210bf644ac7bb5cb05d98b0b17aa258c2c (patch) | |
tree | c988a6c96adf2358db794940f1f1f125fae96201 | |
parent | Add install-bin to default install target (diff) | |
download | dotfiles-bcaaf3210bf644ac7bb5cb05d98b0b17aa258c2c.tar.gz dotfiles-bcaaf3210bf644ac7bb5cb05d98b0b17aa258c2c.zip |
Change cf() from Bash func to shell script
-rw-r--r-- | README.markdown | 2 | ||||
-rw-r--r-- | bash/bashrc.d/cf.bash | 29 | ||||
-rwxr-xr-x | bin/cf | 27 | ||||
-rw-r--r-- | man/man1/cf.1 | 14 |
4 files changed, 42 insertions, 30 deletions
diff --git a/README.markdown b/README.markdown index da0dc44b..40ffb5b2 100644 --- a/README.markdown +++ b/README.markdown @@ -182,7 +182,6 @@ There are a few other little tricks in `bash/bashrc.d`, including: * `apf` -- Prepend arguments to a command with ones read from a file * `bd` -- Change into a named ancestor of the current directory -* `cf` -- Count files in a given directory * `fnl` -- Run a command and save its output and error into temporary files * `hgrep` -- `HISTFILE` search * `keep` -- Permanently store ad-hoc shell functions and variables @@ -297,6 +296,7 @@ Installed by the `install-bin` target: * `ax(1)` evaluates an awk expression given on the command line; intended as a quick way to test how Awk would interpret a given expression. * `ca(1)` prints a count of its given arguments. +* `cf(1)` prints a count of entries in a given directory. * `dub(1)` lists the biggest entries in a directory. * `edda(1)` provides a means to run `ed(1)` over a set of files preserving any options, mostly useful for scripts. diff --git a/bash/bashrc.d/cf.bash b/bash/bashrc.d/cf.bash deleted file mode 100644 index 50308fa3..00000000 --- a/bash/bashrc.d/cf.bash +++ /dev/null @@ -1,29 +0,0 @@ -# Count files -cf() { - local dirname - - # Specify directory to check - dirname=${1:-"$PWD"} - - # Error conditions - if [[ ! -e $dirname ]] ; then - printf 'bash: %s: %s does not exist\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - elif [[ ! -d $dirname ]] ; then - printf 'bash: %s: %s is not a directory\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - elif [[ ! -r $dirname ]] ; then - printf 'bash: %s: %s is not readable\n' \ - "$FUNCNAME" "$dirname" >&2 - return 1 - fi - - # Count files and print; use a subshell so options are unaffected - ( - shopt -s dotglob nullglob - declare -a files=("$dirname"/*) - printf '%u\t%s\n' "${#files[@]}" "$dirname" - ) -} @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +# Count files +self=cf + +# Specify directory to check, defaults to current dir +dirname=${1:-"$PWD"} + +# Error conditions +if [[ ! -e $dirname ]] ; then + printf '%s: %s does not exist\n' \ + "$self" "$dirname" >&2 + exit 1 +elif [[ ! -d $dirname ]] ; then + printf '%s: %s is not a directory\n' \ + "$self" "$dirname" >&2 + exit 1 +elif [[ ! -r $dirname ]] ; then + printf '%s: %s is not readable\n' \ + "$self" "$dirname" >&2 + exit 1 +fi + +# Count files and print; use dotglob and nullglob so we get an accurate count +shopt -s dotglob nullglob +declare -a files=("$dirname"/*) +printf '%u\t%s\n' "${#files[@]}" "$dirname" diff --git a/man/man1/cf.1 b/man/man1/cf.1 new file mode 100644 index 00000000..4c30c9d8 --- /dev/null +++ b/man/man1/cf.1 @@ -0,0 +1,14 @@ +.TH CF 1 "July 2016" "Manual page for cf" +.SH NAME +.B cf +\- print a count of entries in a directory +.SH SYNOPSIS +.B cf +.br +.B cf /path/to/dir +.SH DESCRIPTION +.B cf +counts all the entries in a given directory using Bash glob expansion and +prints the count. It defaults to the current directory. +.SH AUTHOR +Tom Ryder <tom@sanctum.geek.nz> |