Mindtwist.de

...let your mind twist!

How to create a report of the biggest diskhogs on your server

It doesn't matter how much disk space you currently have. Sooner or later your harddisks gets filled to the brim and you need to identify the biggest files on disk that may be candidates for cleanup. The following script that is presented in this article is for all the filesystem janitors out there that want to get a report of their diskhogs.

The Script

 

#! /bin/ksh
# This script creates a list of the biggest files within the specified path.
pathname=$1
if [ -z $pathname ]
then
echo "Error: Please enter a valid path!"
exit
fi

if [ -d $pathname ]
then

clear
echo "List of the biggest files in '$pathname'"
echo "================================================================================"
files=$(du -a "$pathname" 2>/dev/null| sort -rn | head -50 | awk '{print $2}')
echo "$files" | while read file ; do
if [ -f "$file" ]; then
ls -alhs "$file" | read a b c owner e bsize month day t fpath
echo $owner" "$bsize" "$mdate" "$fpath
fi
done | column -t
else
echo "Error: '$pathname' - no such directory!"
exit
fi

The Script in Action

You may either enter a directory that you want to scan for diskhogs, or the script will search beneath the current dir.

The output of the following example has been anonymized. The list shows the user that owns the diskhog, the size and the actual file. Maybe you want to execute the script on the users homedir and place the report in your /etc/motd ;-)

If you need more files in your output, change the "head 50" to e.g. "head 75".

# ./diskhog.sh /usr
List of the biggest files in '/usr/'
================================================================================
root  182M  /usr/lib/**************************somefile
root  54M   /usr/lib/**************************someotherfile
root  43M   /usr/lib/**************************file.dat
root  40M   /usr/lib/**************************data.dat
root  39M   /usr/lib/**************************something.dat
root  30M   /usr/lib/**************************someother.dat
 

Linux Magazine

Linux Magazine News
  • Rocks Releases Mamba

    The latest version of Rocks cluster distribution – an open source toolkit for real and virtual clusters – has been released.

  • PowerTOP Releases v2.0

    PowerTOP releases v2.0 of its Linux tool, with improved diagnostics and user interface.