Shell Script - Find Drive by Size

Discussion in 'Mac Programming' started by hernandito, Sep 29, 2009.

Thread Status:
Not open for further replies.
  1. hernandito macrumors newbie

    Joined:
    Sep 28, 2009
    #1
    This may or may not be a newbie question, but a newbie I am.

    I need guidance defining a command variable that will return the
    Code:
    disk#
    The trick is that I need the script to look at the disks (not partitions) that are between 2GB and 8 GB in size, and return the disk#.

    In plain english, I want for the command to:
    "Tell me the disk number of drives that are between 2Gb and 8Gb. If there is more that one drive that meets the criteria, or if there are none, then just yell "Error"."

    I am going to be doing a dd command to restore an image file to a removable thumb drive and I want to avoid restoring the image to one of the system's hard drives.

    Any help or guidance is greatly appreciated.

    Thank you.
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    No, there is no single command to do so (that I know of, at least). The diskutil tool will be able to provide you with both a list of all known attached disks and the total size of each of those disks but you will need to write a small script to parse out that information. Below is a quick & dirty Perl script that should at least give you a general idea of what you'll need to do.

    Edit: You could also parse out the information from the 'df' command but then you would still have to lookup the device->mountpoint mappings or do some addition.

    Code:
    #!/usr/bin/env perl
    #
    
    @lines = split /\s+/, qx( /usr/sbin/diskutil list );
    
    # Filter out any lines that are not of the form /dev/diskN, where
    #  'N' is a 1 or more digit number, ie: /dev/disk6.
    @disks = grep { $_ =~ /^\/dev\/disk[0-9]{1,}/ } @lines;
    %disk_size = ();
    
    foreach (@disks) {
      my $info_str = qx( /usr/sbin/diskutil info $_ );
      
      # Search for the line that begins with "Total Size:" and strip
      #  out the size of the disk in bytes and store it in a hash.
      # Capturing "\d+\.\d+\s+\w+" instead of the current "\d+"
      #  will give you the size in a human-readable value 
      if( $info_str =~ /Total Size:\s+\d+\.\d+\s+\w+\s+\((\d+)\s+/ ) {
        print "Disk '$_' => $1 Bytes\n";
        $disk_size{ $_ } = $1;
      }
    }
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    The 'df' command provides the sizes of mounted partitions, so you could add them up by identifying the drive. Piping df's output to awk would work. It's more than capable of matching patterns by line, splitting args, and doing arithmetic. It's probably less than a dozen lines of awk.

    Sample data:
    Code:
    df -k
    
    Filesystem                              1K-blocks      Used     Avail Capacity  Mounted on
    /dev/disk0s3                             26083328  16249064   9578264    63%    /
    devfs                                         104       104         0   100%    /dev
    fdesc                                           1         1         0   100%    /dev
    <volfs>                                       512       512         0   100%    /.vol
    /dev/disk0s2                             26083328  16187656   9895672    62%    /Volumes/Foow
    /dev/disk0s4                             26083328  16147632   9935696    62%    /Volumes/Barw2
    /dev/disk0s5                            233592112  38475312 195116800    16%    /Volumes/Work
    automount -nsl [207]                            0         0         0   100%    /Network
    automount -fstab [212]                          0         0         0   100%    /automount/Servers
    automount -static [212]                         0         0         0   100%    /automount/static
    /dev/disk1s3                              1853912    557772   1296140    30%    /Volumes/SD1-HFS
    /dev/disk2s2                              1000904    106232    894672    11%    /Volumes/Xyzzy-HFS
    /dev/disk3s2                             20840448  16696212   4144236    80%    /Volumes/Plugh
    /dev/disk3s4                            348432432 140594912 207837520    40%    /Volumes/Plover
    /dev/disk3s3                             20840448   4040760  16799688    19%    /Volumes/Duh
    afp_2YPUGc00000H0000oM0000VU-1.2c000007 156883120  74910092  81973028    48%    /Volumes/WTF
    
    My main drive is partitioned, and so is /dev/disk3. The others are not.

    Notice the naming pattern (/dev/diskNsM), where 's' stands for "slice", an archaic synonym for "partition". The sizes under the "1K-blocks" heading are the overall size of the slice, measured in 1K blocks. Read the man page for df for other options (GB, MB, 512-byte blocks).
     
  4. hernandito thread starter macrumors newbie

    Joined:
    Sep 28, 2009
    #4
    Thank you guys.... Sadly this is all way above my head... I get the idea of what to do, but I have no idea how to go about it. I am not sure if this is proper here, but I would be happy to pay an expert to assist me with this. I could easily post this in GetAfreelancer.com, or Paypal someone directly. It needs to run inside a shell script.

    Please email me directly if you are interested... hernando(at)econoco.com

    Thanks again!
     
  5. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #5
    Offers to buy are limited to the Marketplace forum. See the forum rules and Marketplace rules.
     
Thread Status:
Not open for further replies.

Share This Page