Back to the Tips Index
Here is a shell script which will show you the MB read and written to each drive mounted on your Mac.

Put the contents of the below script into a file called "driveio" and then execute:

chmod +x driveio

Then you can execute the script by invoking it in Terminal (type its name and press return).


#!/usr/bin/perl

$iostat="iostat -d -K -w 1 -n 9";
if ( !open(IOS, "$iostat|")) {
    print STDERR "cannot open pipe from iostat. $!\n";
    exit;
}

select IOS;
$|=1;           # turn off buffering

select STDOUT;
$|=1;           # turn off buffering

my $got_disknames=0;
my %disknames=&get_disknames;
my @disks;
my $esc=chr(27);

while() {
    if ( /^\s*disk/ ) {
        @disks=();
        foreach $disk ( split(/\s+/, $_)) {
            next if $disk eq '';
            push(@disks, $disknames{$disk});
        }
        $hdrs=;    # throw away the next header line
    } else {
        $_=~s/^\s*//;
        @stats=split(/\s+/, $_);
        foreach $disk ( @disks ) {
           shift(@stats);       # skip KB/t
           shift(@stats);       # skip tps
           $mbs=shift(@stats);
           print "$disk: $mbs MB/s\n";
        }
        my $uplines=$#disks+1;
        my $upstring="$esc\[${uplines}A";
        print $upstring;
    }
}
close(IOS);

sub get_disknames {
    my($disklist)=@_;

    my %disknames=();
    my $dfcmd="df -l";
    if ( !open(DF, "$dfcmd |")) {
        print STDERR "cannot open pipe from df. $!\n";
        exit;
    } else {
        my $disk, $name, $shortdisk;
        # output looks like this:
        #    /dev/disk8s3  312319584 157230104 155089480    50%    /Volumes/foo

        while() {
            if ( /^\/dev\/(\w+).*\/Volumes\/(.*)/ ) {
               $shortdisk=$disk=$1;
               $name=$2;
               $shortdisk=~s/s\d+$//;
               if ( defined($disknames{$shortdisk})) {
                   $disknames{$shortdisk}.=", $name";
               } else {
                   $disknames{$shortdisk}=$name;
               }
            } elsif ( /^\/dev\/(\w+).*(\/$)/ ) {
               $shortdisk=$disk=$1;
               $name=$2;
               $shortdisk=~s/s\d+$//;
               $disknames{$shortdisk}=$name;
            }
        }
        close(DF);
    }

    return(%disknames);
}


All content copyright Howard Cohen 2007 , all rights reserved worlwide.
hoco(at)timefold(dot)com