#!/bin/bash

# Use du to give a byte total of a particular path, then print out the total
# in the SI unit human readable formats.
# Example output:
# > ./d.sh /net/vm
# Total:  39873039000 bytes / 38938514 KB / 38025 MB / 37 GB

# Get the path from the first command line argument, if it is not provided, use
# the current working directory (.):
path=${1:-.}

let total=$(du -sb "$path" | cut -f 1);

echo -n "Total: ";

siunits=( "bytes" "KB" "MB" "GB" "TB" "PB" )

# Setup a index variable for the siunits array defined above:
let si=0;

# While the total is > 0 loop:
while (( $total ))
do
  # if the si index is > 0 output a "/" separator:
  if (( $si )); then echo -n " /"; fi
  # Print the current total with the siunit indexed by the index variable
  echo -n " $total ${siunits[si]}";
  # divide the total by 1024.
  let total/=1024;
  # increment the index variable.
  let si++;
done

# print a new-line after exiting the loop
echo;
