Last active
March 8, 2024 12:25
-
-
Save jpcaparas/35889e5d98317a02d6a35b5804b2776d to your computer and use it in GitHub Desktop.
Find sub-directories with highest inode count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
find . -maxdepth 1 -mindepth 1 -type d -exec sh -c 'echo -n "{}: " ; find "{}" | wc -l' \; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
(Obviously, change
tmp
to what dir you want the script to run)