#!/usr/local/bin/bash # #This script watches a file with du -b (displays the size of the file #in bytes), perhaps useful if you have a slow internet connection and #want to track the progress of a large download. Won't stop until you #press ^C! # Usage is: watchfile filename [delay_interval] # # if delay_interval is not specified, the script pauses 5 seconds # between each time it shows the file size and elapsed time # # # This script uses the date command to get the time of day # so it can print elapsed time. This script does not handle # rollover to a new day! # # Other stuff of interest: # checking for a valid command line parameters # (we expect at least 1 and it must be a file). # # make sure there is at least 1 command line parameter if [ $# -lt 1 ] then echo "usage: watchfile filename [delay_interval]" exit fi # record the file name we are supposed to watch file=$1 # make sure it is really a file if [ ! -f $file ] then echo "Error: $file does not exist or is not a file!" exit fi # this variable is the time we wait between printing the file size # (in seconds) if [ $2 ] then # we got 2 parameters, use the second as the delay interval delay=$2 else delay=5 fi # make sure the delay is really a number # uses expr to find out how many characters in the string $delay # are not digits (anything greater than 0 means it's not # an integer so we quit) if [ `expr $delay : "[^0-9]*"` -gt 0 ] then echo "invalid delay_interval (must be a number greater than 0)" echo "usage: watchfile filename [delay_interval]" exit fi # get the initial hour, min and second ihour=`date +%H` imin=`date +%M` isec=`date +%S` echo "watching the file $1" echo "Starting time ${ihour}:${imin}:${isec}" # now go in to a loop that never ends... while [ 1 ] do # we sleep for delay seconds, but don't rely on # this for printing elapsed time! sleep is not that # accurate (we might sleep much longer) sleep $delay # get the current hour, min and second hour=`date +%H` min=`date +%M` sec=`date +%S` # compute elapsed time dhour=`expr $hour - $ihour` dmin=`expr $min - $imin` dsec=`expr $sec - $isec` elapsed=`expr $dhour "*" 3600 + $dmin "*" 60 + $dsec` echo -n "$elapsed seconds: " kbytes=`du -L -k $file | cut -f1` echo " FILE: " $file " SIZE: " $kbytes "Kbytes" done