Skip to content

Instantly share code, notes, and snippets.

@ljantzen
Created January 5, 2024 10:59
Show Gist options
  • Save ljantzen/cf0d78181b967c688d7a8299add719b2 to your computer and use it in GitHub Desktop.
Save ljantzen/cf0d78181b967c688d7a8299add719b2 to your computer and use it in GitHub Desktop.
Shell script calculating how big percentage a directory takes of total disk space
#!/bin/bash
# Function to calculate the percentage
calculate_percentage() {
total_space=$(df -k --output=size "$1" | sed -n '2p' | tr -d '[:space:]')
used_space=$(du -sk "$1" | cut -f1 | tr -d '[:space:]')
percentage=$(echo "scale=2; ($used_space / $total_space) * 100" | bc)
echo "$percentage $1"
}
# Check if directory paths are provided as arguments
if [ $# -eq 0 ]; then
echo "Usage: $0 /path/to/directory1 /path/to/directory2 ..."
exit 1
fi
# Loop through each provided directory path
for directory_path in "$@"; do
# Check if the provided path is a valid directory
if [ ! -d "$directory_path" ]; then
echo "Error: Not a valid directory path: $directory_path"
else
calculate_percentage "$directory_path"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment