mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-09 01:02:30 +02:00
count returns 0 when no file/directory exists.
This commit is contained in:
parent
d4df1a75fc
commit
6c236cc6c3
41
README.md
41
README.md
|
@ -475,37 +475,30 @@ $ lines ~/.bashrc
|
||||||
|
|
||||||
## Count files or directories in directory
|
## 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:**
|
**Example Function:**
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
count() {
|
count() {
|
||||||
# Usage: count /Example/Dir
|
# Usage: count [.]/Example/Dir/* Count all files.
|
||||||
|
# count [.]/Example/Dir/*/ Count all subdirectories of /Example/Dir
|
||||||
# Returns either number of files/directories found under /Example/Dir
|
# count [.]/Example/Dir/*.jpg Count all files ending in .jpg
|
||||||
# or '-1' if /Example/Dir is not a directory.
|
# count [.]/Example/Dir/*/*.jpg Count all subdirectories containing files ending in .jpg
|
||||||
|
|
||||||
# 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
|
i=0
|
||||||
|
|
||||||
# "${1%/}/" Handles both /Example/Dir and /Example/Dir/
|
# For each expansion of /Example/Dir/*, count if it exists in the file system.
|
||||||
# Generates all items in expansion of /Example/Dir/* and
|
for f in "$@"
|
||||||
# iterates over them in for loop.
|
|
||||||
|
|
||||||
for f in "${1%/}/"*
|
|
||||||
do
|
do
|
||||||
|
|
||||||
# Because "${1%/}/"* is expanded to literal '/Example/Dir/*' when
|
# Because "$@" is expanded to literal '/Example/Dir/*' when
|
||||||
# no match for * is found, we have to test for its existence
|
# no match for * is found, we have to test for the expansions existence
|
||||||
# before tallying it.
|
# before counting it. This will appropriately count a file named '*' in /Example/Dir.
|
||||||
#
|
#
|
||||||
# By changing -e to -{f,d,...} you
|
# By changing -e to -{f,d,...,} the function
|
||||||
# can count only {files,directories,...} or combine
|
# can count only {files,directories,...,} or combine
|
||||||
# tests with '||' to count combinations.
|
# tests with '||' to count combinations.
|
||||||
[ ! -e "$f" ] && continue
|
[ ! -e "$f" ] && continue
|
||||||
i=$(( i + 1 ))
|
i=$(( i + 1 ))
|
||||||
|
@ -513,16 +506,20 @@ count() {
|
||||||
printf '%d\n' $i
|
printf '%d\n' $i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Example Usage:**
|
**Example Usage:**
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
# Count all files and items in dir.
|
# Count any filesystem entry in ~/Downloads.
|
||||||
$ count ~/Downloads/
|
$ count ~/Downloads/*
|
||||||
232
|
232
|
||||||
|
|
||||||
|
# Count subdirectories of ~/Documents.
|
||||||
|
$ count ~/Documents/*/
|
||||||
|
4
|
||||||
|
```
|
||||||
|
|
||||||
## Create an empty file
|
## Create an empty file
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue