views:

308

answers:

3

I'm trying to find some sort of command or regex to get the size and available or used space of a hard drive in linux.

At the moment I'm using this;

df -h

and getting something like this;

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              10G  3.1G  6.4G  33% /
/dev/sda2             147G  5.8G  134G   5% /mnt

What I need is to be able to, per drive, collect the Size, Used and Avail values. Any thoughts on how I could go about collecting that data?

EDIT: Since I apparently wasn't quite clear enough, here's basically what I want the end result to be;

array(
    '/dev/sda1' => array(
        'size' => '10G', 'used' => '3.1G', 'avail' => '6.4G'
        ),
    '/dev/sda2' => array(
        'size' => '147G', 'used' => '5.8G', 'avail' => '134G'
        )
    )

Either that or each partition being in it's own separate array, rather than a master array.

+1  A: 

You already have that information. df -m OR df -k to display it in megabytes or kilobytes, respectively.

If the output isn't exactly what you want, use other command-line tools (grep, sed, awk) to filter and format the output.

Of course, you have to mount the partition to get its used/avail space.

Steven
A: 

If you have root access then you can run fdisk -l /dev/sda to get a list of partitions on a device.

However, I don't see what you could want this information for. If you explained why you need it, that would let people give you better answers.

Zan Lynx
A: 

I figured out the answer to my own question;

Total:

df -h | sed -n 2p | awk '{print $2}'

Free:

df -h | sed -n 2p | awk '{print $4}'

Used:

df -h | sed -n 2p | awk '{print $3}'
Stephen Belanger