Skip to content

Instantly share code, notes, and snippets.

@pn11
Created Aug 30, 2020
Embed
What would you like to do?
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