Home » Questions » Computers [ Ask a new question ]

Create a file whose size in bytes is the percentage of free space on a disk

Create a file whose size in bytes is the percentage of free space on a disk

I needed a command-line method of creating a file whose size in bytes is the percentage of free space on a disk. I intend to run it via cron every five minutes, but first a shell script will do.

Asked by: Guest | Views: 296
Total answers/comments: 1
Guest [Entry]

"I would use dd. You say ""lvolfs -a (not a BSD command)"". Do you have df? That would be a more standard and portable way to get the volume usage information.

It shouldn't be necessary to create a temporary script file to do this. You should be able to do:

dd if=/dev/rdsk of=//tr2/test/data/tr2.free bs=\
$(/com/lvolfs -a | awk -e '/4D72C/ { print $3 }') count=1

or

avail=$(df /usr | awk -e 'NR==2 { print $5 }')
avail=${avail%*%} # strip off the percent sign
dd if=/dev/rdsk of=//tr2/test/data/tr2.free bs=$avail count=1

(depending on how your filesystems are organized and what the output of df looks like).

You could even put the second one in a loop:

for fs in / /usr /home # list the ones you want to include
do
avail=$(df $fs | awk -e 'NR==2 { print $5 }')
avail=${avail%*%} # strip off the percent sign
# create a file on the particular filesystem/mount point
dd if=/dev/rdsk of=//$fs/test/data/tr2.free bs=$avail count=1
done

Examine this carefully before you use it to make sure it does what you intend."