Skip to content

Instantly share code, notes, and snippets.

@hongkongkiwi
Last active May 24, 2023 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongkongkiwi/ade25ad349c243aa62871be87b13b9cb to your computer and use it in GitHub Desktop.
Save hongkongkiwi/ade25ad349c243aa62871be87b13b9cb to your computer and use it in GitHub Desktop.
Simple shell script to count all files (or directories) in current directory, optionally can pass in a pattern for find
#!/bin/sh -u
# Count all files
# ./count
# Count only mp3 files
# ./count "*.mp3"
# Count only directories
# ./count "" "d"
# Count only directories ending with blah
# ./count "*blah" "d"
PATTERN="${1:-"*"}"
COUNT_TYPE="${2:-"f"}"
exec find * -maxdepth 1 -type "$COUNT_TYPE" -name "$PATTERN" | wc -l | tr -d ' '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment