bash script to determine data sizes

A large majority of mac users are illustrators, photographers, graphic designers. They work with graphics design suites, 3D modeling software, post-production video editing suites, and often have very large amounts of data stored on their hard drives. Inevitably, their computer will need to be refreshed with a newer, faster model. A challenge I run into regularly is how much data is the refresh project team going to actually have to migrate? Unfortunately for us, FW800 (our preferred data transfer method) is just not fast enough to transfer the hundreds of gigs of data that some of these users have in a timely manner. In order to (re)set turnaround expectations for department managers and bean counters, it’s useful to have some hard numbers when it comes to data transfer rates and sizes.

I wrote a bash script to to capture the currently logged in users’ data sizes as well as any data kept outside of the /Users folder (i.e.: data shared amongst many users) and write it to a text file in easy to read format. I also included a line for easy parsing into spreadsheet formats for our projects’ BAs to crunch into colorful graphs 😉  It works nicely as a 1.0 script.

#!/bin/sh

###########################################################################
# ScanWorkDriveSize.sh written by Andrew Caldwell 3/15/11. This script    #
# runs on each mac and captures Work drive size as well as logged in User #
# size. It writes this info to a txt file and saves to MacPackaging.      #
###########################################################################

## Note that this script captures block size not byte size so these numbers are not totally accurate, but pretty close

## Get computer name
COMPNAME=`scutil --get ComputerName`

## Get the size of the used portion of the work drive - du appends the folder name, so we'll have to cut that later
RAWWORKDRIVESIZE=`du -sk /Volumes/Work/`

## Get the size of the /Users/Shared folder for comparison - du appends the folder name, so we'll have to cut that later
RAWSHAREDFOLDERSIZE=`du -sk /Users/Shared/`

## Get the entire user folder size (for easier calculations) - du appends the folder name, so we'll have to cut that later
RAWTOTALUSERFOLDERSIZE=`du -sk /Users/`

## Get the currently logged in user
CURRENTUSER=`who | grep console | cut -d\  -f1`

## Get the size of their home folder - du appends the folder name, so we'll have to cut that later
RAWCURRENTUSERFOLDERSIZE=`du -sk /Users/$CURRENTUSER`

## Calculate approx. data transfer size
## All folder sizes were read as a common unit since we don't know if they are MB, GB, etc. KB gives us the most accurate sizing available in lieu of Bytes.
## The script will convert all sizes to GB for output...

## First cut the end off each of the du statements to get integer variables. Cut is used to capture everything before the '\ ' delimiter
WORKDRIVESIZE=`echo $RAWWORKDRIVESIZE | cut -f 1 -d\ `
SHAREDFOLDERSIZE=`echo $RAWSHAREDFOLDERSIZE | cut -f 1 -d\ `
TOTALUSERFOLDERSIZE=`echo $RAWTOTALUSERFOLDERSIZE | cut -f 1 -d\ `
CURRENTUSERFOLDERSIZE=`echo $RAWCURRENTUSERFOLDERSIZE | cut -f 1 -d\ `

## Now calculate how many KB blocks need to be transfered. We are looking to transfer only current user + any common/shared folders
let XFERSIZE=$WORKDRIVESIZE-$TOTALUSERFOLDERSIZE+$CURRENTUSERFOLDERSIZE+$SHAREDFOLDERSIZE

## Now translate this into GB blocks
let XFERSIZEGB=$XFERSIZE/1024/1024

## Write all this information to a text file for later processing
echo "$COMPNAME" >> /Users/Shared/$COMPNAME-WorkDriveSize.txt
echo "SPACE USED ON WORK: $WORKDRIVESIZE KB" >> /Users/Shared/$COMPNAME-WorkDriveSize.txt
echo "$CURRENTUSER HOME FOLDER SIZE: $CURRENTUSERFOLDERSIZE KB" >> /Users/Shared/$COMPNAME-WorkDriveSize.txt
echo "USERS/SHARED FOLDER SIZE: $SHAREDFOLDERSIZE KB" >> /Users/Shared/$COMPNAME-WorkDriveSize.txt
echo "APPROX TRANSFER SIZE: $XFERSIZEGB GB" >> /Users/Shared/$COMPNAME-WorkDriveSize.txt

## Add a line at the end to make parsing much easier
echo "$COMPNAME,$WORKDRIVESIZE,$CURRENTUSER,$CURRENTUSERFOLDERSIZE,$SHAREDFOLDERSIZE,$XFERSIZEGB" >> /Users/Shared/$COMPNAME-WorkDriveSize.txt

mkdir -p /Volumes/MacPackaging
mount_smbfs //yyyjamf:cA5P3r@netappv2/MacPackaging /Volumes/MacPackaging

## Copy the text file to MacPackaging/FY12MACDAC-DataTransferInfo
cp /Users/Shared/$COMPNAME-WorkDriveSize.txt /Volumes/MacPackaging/FY12MACDAC-DataTransferInfo/
sleep 1

## Clean up
rm /Users/Shared/$COMPNAME-WorkDriveSize.txt

About this entry