Skip to content

Instantly share code, notes, and snippets.

@jpcaparas
Last active March 8, 2024 12:25
Show Gist options
  • Save jpcaparas/35889e5d98317a02d6a35b5804b2776d to your computer and use it in GitHub Desktop.
Save jpcaparas/35889e5d98317a02d6a35b5804b2776d to your computer and use it in GitHub Desktop.
Find sub-directories with highest inode count
#!/bin/bash
find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'echo -n "{}: " ; find "{}" | wc -l' \;
#!/bin/bash
# Recursive function to print directory tree with inode counts
# Defaults to 2 directories deep
print_tree() {
local current_dir="$1"
local indent="$2"
local depth="$3"
local max_depth="$4"
# Calculate inode count for the current directory
local count=$(find "$current_dir" | wc -l)
# Print current directory and its inode count with indentation
echo "${indent}${current_dir##*/}: $count"
# Stop recursion if max depth reached
if [ "$depth" -ge "$max_depth" ]; then
return
fi
# Increment depth and prepare new indentation for subdirectories
local new_indent="$indent "
local new_depth=$((depth + 1))
# Loop through subdirectories and recursively print their trees
find "$current_dir" -mindepth 1 -maxdepth 1 -type d | while read subdir; do
print_tree "$subdir" "$new_indent" "$new_depth" "$max_depth"
done
}
# Parse command line arguments
base_directory="${1:-.}" # Default to current directory if not specified
max_depth="${2:-2}" # Default to 5 levels deep if not specified
# Validate max_depth is a number
if ! [[ "$max_depth" =~ ^[0-9]+$ ]]; then
echo "Error: Depth must be a number."
exit 1
fi
# Initial call to the print_tree function
print_tree "$base_directory" "" 0 "$max_depth"
@jpcaparas
Copy link
Author

jpcaparas commented Mar 8, 2024

Usage:

curl -sL https://gist.githubusercontent.com/jpcaparas/35889e5d98317a02d6a35b5804b2776d/raw/78b6df29cd002ee03b144285c80bfefcac86f5b8/inode-count-recursive.sh | bash -s --
/tmp | less

(Obviously, change tmp to what dir you want the script to run)

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