Skip to content

Instantly share code, notes, and snippets.

@peabnuts123
Created May 17, 2019 00:30
Show Gist options
  • Save peabnuts123/0220b56b775e05a22053b5ad417a28f1 to your computer and use it in GitHub Desktop.
Save peabnuts123/0220b56b775e05a22053b5ad417a28f1 to your computer and use it in GitHub Desktop.
Bash utility function for listing the total (recursive) count of files within all subdirectories
# Usage: ls-count (directory=.) (depth=1)
# Perform a recursive find in the specified folder, and display
# a summary of the total number of files in each subfolder
#
# If `directory` is specified, perform search in that directory. Defaults to '.'
# If `depth`is specified, output analysis down to `depth` levels from root. Defaults to 1
# Example usage:
# ls-count
# ls-count ~/Documents
# ls-count ~/Documents/Projects 2
function ls-count() {
local directory="${1:-.}";
local maxdepth="${2:-1}";
find "${directory}" -type d -maxdepth "${maxdepth}" -mindepth 1 | while read x; do echo "$(find "$x" -type f -mindepth 1 | wc -l) $x"; done | sort -nr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment