Bash Script to Monitor Space by Directory.

Neil Brown brownn0-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Thu Dec 10 15:32:31 UTC 2009


I had this old script from work kicking around.  It lists the largest n 
files in each directory in the specified path.  It was originally 
written for Korn shell, but is ok for bash.

lm -h will give a brief usage message.
lm -5 /home will give the 5 largest files in each directory in /home, etc.


#!/bin/bash

#d list largest [-n] files in each directory in path

# defaults:

#    -n     1
#    -s     1 block ( 1024 bytes per block )
#    [path] . ( current directory )

USAGE="usage: lm [ -n n ] [ -s n ] [path]"

# default parameters

nh=1;                   # number of files
MAX=`expr 1 \* 1024`            # max size of file
spath=".";              # specified path

# command-line parameters

while getopts hn:s: c
do
case $c in
     h)  usage $0 $USAGE;            # display help text
     exit ;;
     n)  nh=$OPTARG;;
     s)  MAX=`expr $OPTARG \* 1024`;;
     ?)  usage $0 $USAGE;
     exit 1;;
esac
done

shift `expr $OPTIND - 1`;       # search path

if [ $# -eq 1 ]
then
     spath=$1;
fi

(

  # for i in ( all dirs )

  for i in `ls -FR $spath 2> /dev/null | sed -n 's/:$//p'`
  do

      c=`ls -l $i | sort -r -n +4 -5 | head -1 | awk '{ print $5; }'`;
      if [ "$c" -gt $MAX ]
      then

      for j in `ls -l $i | awk -v max=$MAX '$5>=max { print $NF; }'`
      do
          ls -l $i/$j;
      done

      fi

  done

  ) 2> /dev/null | pg;

I hope this helps.


Neil Brown        brownn0-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org

Toronto, Ontario, Canada

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://gtalug.org/pipermail/legacy/attachments/20091210/c7e235ce/attachment.html>


More information about the Legacy mailing list