Created
December 11, 2023 09:52
-
-
Save cedricbahirwe/7eda4e84b64ba374faec45b3c97cd4c1 to your computer and use it in GitHub Desktop.
This shell script finds and displays the top N heaviest subdirectories in a given directory, where N is provided by the user as a command-line argument.
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 | |
# Check if the correct number of arguments is provided | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 <directory> <number_of_top_directories>" | |
exit 1 | |
fi | |
directory="$1" | |
top_count="$2" | |
# Check if the provided directory exists | |
if [ ! -d "$directory" ]; then | |
echo "Error: Directory '$directory' not found." | |
exit 1 | |
fi | |
# Find and print the top N heaviest subdirectories | |
top_heaviest_subdirectories=$(du -h -d 1 "$directory" | sort -rh | awk -v n="$top_count" 'NR<=n+1 {print $2}') | |
echo "Top $top_count heaviest subdirectories in '$directory':" | |
echo "$top_heaviest_subdirectories" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment