Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Checking Disk Space

Shell scripts are commonly used to keep system administrators up to date about the amount of free space available in certain directories. For example, you don’t want the incoming mail directory to fill up. An auxiliary task is to determine how much space a directory uses. For example, you don’t want a single user to hog up all the disk space for extended periods.

The information about disk usage is also important to installation scripts because they need to warn a user when an installation is attempted in a directory that does not contain enough space.

In this section, I will present two functions that can help you determine disk space usage:

  getSpaceFree
  getSpaceUsed

Determining Free Space

To determine the free space in a directory, you use the df -k (k as in KB) command. Its output looks like the following:

$ df -k
Filesystem         1024-blocks  Used Available Capacity Mounted on
/dev/hda1            1190014  664661   463867     59%   /
/dev/hdd1            4128240 1578837  2335788     40%   /internal
/dev/hdb1            1521567  682186   760759     47%   /store
/dev/hda3             320086   72521   231034     24%   /tmp

When run on a single directory or file, the output looks like the following:

$ df -k /home/ranga
Filesystem         1024-blocks  Used Available Capacity Mounted on
/dev/hda1            1190014  664661   463867     59%   /

The output consists of a header line and information about the hard drive or hard drive partition that the directory or file you specified is located on. In this output, the amount of free space is stored in the fourth column. Your function uses awk to get at this value.

################################################
# Name: getSpaceFree
# Desc: output the space avail for a directory
# Args: $1 -> The directory to check
################################################

getSpaceFree() {

    if [ $# -lt 1 ] ; then
        printERROR "Insufficient Arguments."
        return 1
    fi

    df -k "$1" | awk 'NR != 1 { print $4 ; }'
}

As you can see, the function is quite simple. It first checks to see whether it was given an argument. If no argument was given, it displays an error message and returns. Otherwise, it runs the df -k command and displays the number stored in the fourth column. You use the awk expression

NR != 1

to skip the first line that contains the header. For more information on awk, please look at Chapter 17, “Filtering Text with awk.”

As an example of using this function, the command

getSpaceFree /usr/local

displays the following value on my system:

2335788

The number returned is in kilobytes, which means I have about 2.3GB free in the directory /usr/local.

Frequently you might want to compare the output of this function to some value. For example, the following if statement checks to see whether more than 20,000KB are available in the directory /usr/local:

if [ "`getSpaceFree /usr/local`" -gt 20000 ] ; then
    echo "Enough space"
fi

Determining Space Used

You have looked at determining the amount of disk space available in a directory, but sometimes you need to know how much disk space a directory uses. For example, you might have a public directory that needs to be cleaned out when it exceeds a certain size.

To perform this task, you need to use the du (du as in disk usage) command. Because you want the output for an entire directory and its contents, you need to specify the -s (s as in sum) option to du. You also need to specify the -k (k as in kilobyte) option for a consistent output on all versions of UNIX.

The output of the du -sk command looks like the following:

$ du -sk /home/ranga/pub
4922    /home/ranga/pub

The size of the directory in kilobytes is listed in the first column. Your function uses awk to obtain this number.

################################################
# Name: getSpaceUsed
# Desc: output the space used for a directory
# Args: $1 -> The directory to check
################################################

getSpaceUsed() {

    if [ $# -lt 1 ] ; then
        printERROR "Insufficient Arguments."
        return 1
    fi

    if [ ! -d "$1" ] ; then
        printERROR "$1 is not a directory."
        return 1
    fi

    du -sk "$1" | awk `{ print $1 ; }'
}

This function is almost as simple as getSpaceFree. It first checks whether it was given an argument. If no argument was given, it displays an error message and returns. Otherwise, it checks to see whether the first argument is a directory. If it is not, an error message is displayed and the function returns.

Otherwise, the function executes the du -sk command and displays the number stored in the first column.

As an example of using this function, the command

getSpaceUsed /usr/local

displays the following value on my system:

15164

The number returned is in kilobytes, which means that the directory /usr/local uses about 15.1MB.

Frequently, you’ll want to compare the output of this function to some value. For example, the following if statement checks to see whether more than 10,000KB are used by the directory /home/ranga/pub:

if [ "`getSpaceSpace /home/ranga/pub'" -gt 10000 ] ; then
    printWARNING "You're using to much space!"
fi


Previous Table of Contents Next