Checking disk space

By Sandra Henry-Stocker  Add a new comment

Here's a script for quickly checking on how much disk space you have on a system and how much of it is in use. It certainly beats using a calculator to tally up the columns in "df -k" output.

We're not looking for precision here. After all, disk usage changes very frequently. We don't want to see this information extended to ten decimal places. We just want a "feel" for how much space is in use and how much is still available. So, we report disk space in gigabytes and calculate a percentage. We round up both the total disk space and disk space used to the next gigabyte and the percentage used to the next percent. The generated report will look something like this:

# ./chkDiskSpace
total: 308 GB
 used: 32 GB
11% in use

The script includes two subroutines -- one to round up a number and one to convert kilobytes to gigabytes. We could have used the "df -h" to report disk usage in GB instead of KB, but then we'd have to concern ourselves with the decimal points and occassional datapoints reported in KB because of their small size and that would complicate everything.

We squeeze strings of blanks into single blanks so that we can split the "df -k" output on the blanks in between the columns and skip over the headings before we run through each line of data.

The script also skips over swap space and NFS mounted partitions by ignoring any lines in the "df -k" output that don't start with the tell-tale "/dev". It also doesn't report on unmounted file systems; it's only looking at mounted drives.

#!/usr/bin/perl

# round up to next integer
sub roundup {
    my $n = shift;
    return(($n == int($n)) ? $n : int($n + 1))
}

sub conv2g {
    my $n = shift;

    return($n/1000000);
}

# get disk space report
open(STATS, "df -k|") || die "$!";

# init params
my $lineNo  = 0;
my $totUsed   = 0;
my $totSpace  = 0;

while ($input = <STATS>) {
  if ($lineNo++ != 0) {         # Skip column headings
    $input =~ s/\s+/ /g;        # squeeze out blanks
    next if $input !~ /^\/dev/; # skip over NFS mounts, swap
    my @columns = split(/ /, $input);
    $totSpace += $columns[1];
    $totUsed += $columns[2];
  };
};

$percent = $totUsed / $totSpace;
$pct=roundup($percent * 100);
$totSpace=roundup(conv2g($totSpace));
$totUsed=roundup(conv2g($totUsed));

print "total: $totSpace GB\n";
print " used: $totUsed GB\n";
print "$pct% in use\n";

You could set up a script like this to email you the results or email you the results only if the percentage of free disk space warrants your attention. Alternately, you might just use it when you're making plans for upgrading disks on some of your file servers and want a quick report on whether they're hurting for space or should be in good shape for the next year or two.

ITworld LIVE

IT Management/StrategyWhite Papers & Webcasts

White Paper

The Cloud: Reinventing Enterprise Collaboration

Collaboration and content sharing are not, of course, new concepts. But cloud computing has changed the nature of collaboration, content sharing, document storage and project management to enable more efficient, faster-acting and cost-effective enterprises. According to a new study by IDG Research, the vast majority of knowledge workers (86%) placed a very high level of importance on collaborating with internal coworkers and external stakeholders, and having access to the most up-to-date corporate information. Read how organizations are realizing massive productivity gains by transitioning their content management solutions to cloud-based models.

White Paper

Empowering Your Mobile Worker

Today's most productive employees are mobile, and your company's IT strategy must be ready to support them with 24/7 access to the business information they need across a range of mobile devices.See how corporations are meeting the many needs of their mobile workers with the help of Box.

White Paper

Market Landscape Report: Online File Sharing and Collaboration in the Enterprise

The trend toward "consumerization" marches onward in IT; more and more end-users are choosing their own hardware plaforms and software applications in lieu of the IT-sanctioned business tools provided by their companies. These end-users are looking to tackle issues like data sharing, portability, and access from multiple intelligent endpoint devices, creating a conundrum for IT as it needs to balance business enablement, ease of access, and collaborative capacity with the need to maintain control and security of information assets. This need for balance is one of the drivers of the fast growing online file sharing and collaboration segment of the SaaS market. This paper examines the market drivers, inhibitors, and top vendors in this segment, including Box, Citrix Sharefile, Dropbox, Egnyte, Nomadesk, Sugarsync, Syncplicity and YouSendIt.

White Paper

Sharing Simplified - Consolidating File-sharing Technologies

Employees need to share content with colleagues within their organization and outside. Yet, ECMs make it hard to share content within a business and impossible between organizations. Read how one company consolidated multiple file sharing technologies to increase productivity and reduce complexity.

White Paper

Content Sharing 2.0: The Road Ahead

A growing number of companies are taking advantage of the natural synergies that exist between cloud-based IT services and content access and sharing. Legacy content management and collaboration systems simply weren't designed to meet the evolving requirements of today's IT and business managers, as well as the needs of content users. Box provides cloud-based content storage, access and collaboration services that require virtually no user training and supports file access and delivery on almost all popular PC and mobile devices. Read how Box let companies rapidly implement a cost-effective and secure content storage and sharing system that can easily expand to accommodate any size and number of files.

See more White Papers | Webcasts

Ask a question

Ask a Question