From d4df1a75fcafd45cae933081a2ae810a79421b53 Mon Sep 17 00:00:00 2001 From: melangston <61057176+melangston@users.noreply.github.com> Date: Fri, 14 Feb 2020 11:33:28 -0500 Subject: [PATCH 1/3] Update README.md --- README.md | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 6765de4..729e755 100644 --- a/README.md +++ b/README.md @@ -475,35 +475,54 @@ $ lines ~/.bashrc ## Count files or directories in directory -This works by passing the output of the glob to the function and then counting the number of arguments. +This iterates over every item in the glob argument/* and tallies them if they exist. -**CAVEAT:** When the glob does not match anything (empty directory or no matching files) it is not expanded and the function returns `1`. **Example Function:** ```sh count() { - # Usage: count /path/to/dir/* - # count /path/to/dir/*/ - printf '%s\n' "$#" + # Usage: count /Example/Dir + + # Returns either number of files/directories found under /Example/Dir + # or '-1' if /Example/Dir is not a directory. + + # Test if /Example/Dir actually exists and is a directory. + # Return -1 if not an existing directory. + [ -d "$1" ] || { printf '%d\n' -1 ; exit ; } + + i=0 + + # "${1%/}/" Handles both /Example/Dir and /Example/Dir/ + # Generates all items in expansion of /Example/Dir/* and + # iterates over them in for loop. + + for f in "${1%/}/"* + do + + # Because "${1%/}/"* is expanded to literal '/Example/Dir/*' when + # no match for * is found, we have to test for its existence + # before tallying it. + # + # By changing -e to -{f,d,...} you + # can count only {files,directories,...} or combine + # tests with '||' to count combinations. + [ ! -e "$f" ] && continue + i=$(( i + 1 )) + done + printf '%d\n' $i } + ``` **Example Usage:** ```shell -# Count all files in dir. -$ count ~/Downloads/* +# Count all files and items in dir. +$ count ~/Downloads/ 232 -# Count all dirs in dir. -$ count ~/Downloads/*/ -45 -# Count all jpg files in dir. -$ count ~/Pictures/*.jpg -64 -``` ## Create an empty file From 6c236cc6c3951905d623538b69bf12ba6441446e Mon Sep 17 00:00:00 2001 From: melangston <61057176+melangston@users.noreply.github.com> Date: Sat, 15 Feb 2020 08:57:44 -0500 Subject: [PATCH 2/3] count returns 0 when no file/directory exists. --- README.md | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 729e755..0f0adfd 100644 --- a/README.md +++ b/README.md @@ -475,37 +475,30 @@ $ lines ~/.bashrc ## Count files or directories in directory -This iterates over every item in the glob argument/* and tallies them if they exist. +Using pathname expansion to generate a list of filesystem entries that match a pattern and count them. **Example Function:** ```sh count() { - # Usage: count /Example/Dir - - # Returns either number of files/directories found under /Example/Dir - # or '-1' if /Example/Dir is not a directory. - - # Test if /Example/Dir actually exists and is a directory. - # Return -1 if not an existing directory. - [ -d "$1" ] || { printf '%d\n' -1 ; exit ; } + # Usage: count [.]/Example/Dir/* Count all files. + # count [.]/Example/Dir/*/ Count all subdirectories of /Example/Dir + # count [.]/Example/Dir/*.jpg Count all files ending in .jpg + # count [.]/Example/Dir/*/*.jpg Count all subdirectories containing files ending in .jpg i=0 - # "${1%/}/" Handles both /Example/Dir and /Example/Dir/ - # Generates all items in expansion of /Example/Dir/* and - # iterates over them in for loop. - - for f in "${1%/}/"* + # For each expansion of /Example/Dir/*, count if it exists in the file system. + for f in "$@" do - # Because "${1%/}/"* is expanded to literal '/Example/Dir/*' when - # no match for * is found, we have to test for its existence - # before tallying it. + # Because "$@" is expanded to literal '/Example/Dir/*' when + # no match for * is found, we have to test for the expansions existence + # before counting it. This will appropriately count a file named '*' in /Example/Dir. # - # By changing -e to -{f,d,...} you - # can count only {files,directories,...} or combine + # By changing -e to -{f,d,...,} the function + # can count only {files,directories,...,} or combine # tests with '||' to count combinations. [ ! -e "$f" ] && continue i=$(( i + 1 )) @@ -513,16 +506,20 @@ count() { printf '%d\n' $i } + ``` **Example Usage:** ```shell -# Count all files and items in dir. -$ count ~/Downloads/ +# Count any filesystem entry in ~/Downloads. +$ count ~/Downloads/* 232 - +# Count subdirectories of ~/Documents. +$ count ~/Documents/*/ +4 +``` ## Create an empty file From 26ea7fbd3d082a0146946681a32e8ac57c105c1e Mon Sep 17 00:00:00 2001 From: melangston <61057176+melangston@users.noreply.github.com> Date: Sat, 15 Feb 2020 08:58:42 -0500 Subject: [PATCH 3/3] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0f0adfd..6a48bc4 100644 --- a/README.md +++ b/README.md @@ -482,9 +482,9 @@ Using pathname expansion to generate a list of filesystem entries that match a p ```sh count() { - # Usage: count [.]/Example/Dir/* Count all files. - # count [.]/Example/Dir/*/ Count all subdirectories of /Example/Dir - # count [.]/Example/Dir/*.jpg Count all files ending in .jpg + # Usage: count [.]/Example/Dir/* Count all files. + # count [.]/Example/Dir/*/ Count all subdirectories of /Example/Dir + # count [.]/Example/Dir/*.jpg Count all files ending in .jpg # count [.]/Example/Dir/*/*.jpg Count all subdirectories containing files ending in .jpg i=0