Created
September 9, 2024 03:59
-
-
Save naranyala/18e7468cf045baa11347dfd7777cbd56 to your computer and use it in GitHub Desktop.
intuitive command to show file or directory size
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 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