Friday, August 3, 2012

'du' command with slimmed down response

Below is a bash script I use to slim down the response from the 'du' command. I've named the script du.sh . Try it out.




#!/bin/bash
if [ $# -lt 1 ] # check command line parameters
then
   echo ""
   echo "usage: `basename $0` param_1"
   echo ""
   echo "parameter: "
   echo "    path to directory"
   echo "     "
   exit 1
else # translate command line parameters to vars
   # define vars
   DIR=$1

  # ensure there is no trailing /
  pathsuffix=$DIR
  [ ${pathsuffix:0:1} != "/" ]
  pathsuffix=${pathsuffix%/}

  # add a trailing slash
  DIR="$pathsuffix"

  # $1 is the directory to use for getting size
  du -h "$DIR" | grep --color=always "$DIR""/[0-9a-zA-Z\-\_\.]\+\$"
fi