Skip to content

Instantly share code, notes, and snippets.

@pn11
Created August 30, 2020 04:52
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 pn11/c7ee296fe6d63d33ab6ce4ef13334f30 to your computer and use it in GitHub Desktop.
Save pn11/c7ee296fe6d63d33ab6ce4ef13334f30 to your computer and use it in GitHub Desktop.
Count files in a directory recursively with Bash
#!/bin/bash
# For Mac (Homebrew), use aliases below
#alias find=gfind
#alias sed=gsed
function count_files () {
num_files=$(gfind "$1" -maxdepth 1 -type f | wc -l)
echo "$1 ${num_files}"
find "$1" -maxdepth 1 -type d | tail -n +2 | while read line; do
count_files "$line"
done
}
count_files $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment