Skip to content

Instantly share code, notes, and snippets.

@ryanshoover
Last active August 18, 2023 02:18
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 ryanshoover/5aabecb03a193ac9625c73134e3b2975 to your computer and use it in GitHub Desktop.
Save ryanshoover/5aabecb03a193ac9625c73134e3b2975 to your computer and use it in GitHub Desktop.
Flag directories with > 30,000 files
find /path/to/starting/directory -mindepth 1 -type d -exec bash -c 'echo -n "{}: "; find "{}" -maxdepth 1 -type f | wc -l' \;
#!/bin/bash
# Usage:
# ./count-files.sh <directory>
# Function to count files in a directory
count_files() {
local dir="$1"
local num_files=$(find "$dir" -type f | wc -l)
if [ $num_files -gt 30000 ]; then
echo "$dir: $num_files"
fi
}
# Function to recursively count files in directories
recursive_count_files() {
local start_dir="$1"
if [ -d "$start_dir" ]; then
count_files "$start_dir"
for d in "$start_dir"/*; do
if [ -d "$d" ]; then
recursive_count_files "$d"
fi
done
fi
}
# Usage: ./count_files_recursive.sh /path/to/start/directory
# Check if an argument is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 /path/to/start/directory"
exit 1
fi
start_directory="$1"
recursive_count_files "$start_directory"
@aveeesy
Copy link

aveeesy commented Aug 3, 2023

Thank you, Ryan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment