Skip to content

Instantly share code, notes, and snippets.

@naranyala
Created September 9, 2024 03:59
Show Gist options
  • Save naranyala/18e7468cf045baa11347dfd7777cbd56 to your computer and use it in GitHub Desktop.
Save naranyala/18e7468cf045baa11347dfd7777cbd56 to your computer and use it in GitHub Desktop.
intuitive command to show file or directory size
#!/bin/bash
# Check if an argument (file or directory) is provided
if [ -z "$1" ]; then
echo "Usage: sizeof <file_or_directory>"
exit 1
fi
# Use du to calculate size in bytes, then dynamically adjust for KB, MB, GB, etc.
size_in_bytes=$(du -sb "$1" | cut -f1)
if [ "$size_in_bytes" -lt 1024 ]; then
echo "$size_in_bytes bytes"
elif [ "$size_in_bytes" -lt $((1024**2)) ]; then
echo "$((size_in_bytes / 1024)) KB"
elif [ "$size_in_bytes" -lt $((1024**3)) ]; then
echo "$((size_in_bytes / 1024**2)) MB"
else
echo "$((size_in_bytes / 1024**3)) GB"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment