Disk Usage - Space Used

Hi all,

FreeBSD7.1 @ sh.

In a backup script I am trying to get the blocks used by the backup once completed.

I am using the function:

#!/bin/sh
spaceused()
{
 du -d 0 "${1}" | awk -F"[ \t]+" '{ print $1 }
}

to return the blocks used of said directory and contents.

Via. command line it works perf, However in the backup script it returns exactly double the blocks used ...
ie: if there are 459552 blocks used it returns 919104!

If I include "-h" (as below) it does return the correct value.

#!/bin/sh
spaceused()
{
  du -h -d 0 "${1}" | awk -F"[ \t]+" '{ print $1 }
}

I am calling it like:

USED=$(spaceused /the/directory)
echo "Blocks used: ${USED}"

This baffles me, Any ideas why this happens.

Ultimately I'm after the bytes used.

Thanks All!

-Enjoy
fh : )_~

i think you're missing a single qout

du -h -d 0 "${1}" | awk -F"[ \t]+" '{ print $1 }'

oooPs, Sorry, My bad, I missed the last char on the copy selection, It's NOT missing in the script, just in the post.

Thanks for pointing it out though!

I have changed:

du -h -d 0 "${1}" | awk -F"[ \t]+" '{ print $1 }'

To

du -s "${1}" | cut -f1

Unsure of pro's/con's of either method.

Thanks for doing what ya do!

-Enjoy
fh : )_~

Hi all,

A little more on this issue that is baffling.

The following code produces two different results depending on if it's run from the command line or from a cron job...

From a cron job it is exactly twice the block count.

Any ideas??

Command line:

sh SpaceUsed.sh

Cron job:

30	4	*	*	*	root	sh /location/SpaceUsed.sh

Example output:

From Command line
 Space used: 912924
From crontab
 Space used: 1825848

The Code

#!/bin/sh

#set -x

TARGET="/path"
LOGNAME="test.log"

SpaceUsed()
{
  # for demo simplicity
  du -s "${1}" | cut -f1
#  echo $(($(du -s "${1}" | cut -f1) * 1024)) | sed -e :x -e 's/\([0-9][0-9]*\)\([0-9][0-9][0-9]\)/\1,\2/' -e 'tx'
}

echo " Space used: $(SpaceUsed ${TARGET})" >> ${TARGET}/${LOGNAME}
exit 0

Thanks all!

-Enjoy
fh : )_~

Maybe someone has aliased "du" as "du -k" ?
Try typing "alias" at the command prompt.

Similarly there may be a script called "du".
Try typing "whence du" at the command prompt.

Thanks to no one here this has been solved!

It was a unset BLOCKSIZE issue.

I _assumed_ user crontab used the users environment, that is false.

If all else fails be EXPLICIT!

-Enjoy
fh : )_~

---------- Post updated at 11:54 AM ---------- Previous update was at 11:49 AM ----------

Thanks methyl,

I was posting the solution as you were posting this suggestion.

-Enjoy
fh : )_~

Try a one-time cron (not an "at" job) containing just and "env" command. That will show how little is present in cron's environment.

Ah Ha! Great educational tip... Will do!

Thanks

-Enjoy
fh : )_~