Visit Our UNIX and Linux User Community


Disk Usage - Space Used


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Disk Usage - Space Used
# 1  
Old 07-30-2009
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:
Code:
#!/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.
Code:
#!/bin/sh
spaceused()
{
  du -h -d 0 "${1}" | awk -F"[ \t]+" '{ print $1 }
}

I am calling it like:
Code:
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 : )_~
# 2  
Old 07-30-2009
i think you're missing a single qout
Code:
du -h -d 0 "${1}" | awk -F"[ \t]+" '{ print $1 }'

# 3  
Old 07-30-2009
Quote:
Originally Posted by ryandegreat25
i think you're missing a single qout
Code:
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:
Code:
du -h -d 0 "${1}" | awk -F"[ \t]+" '{ print $1 }'

To
Code:
du -s "${1}" | cut -f1

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

Thanks for doing what ya do!

-Enjoy
fh : )_~
# 4  
Old 08-02-2009
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:
Code:
sh SpaceUsed.sh

Cron job:
Code:
30	4	*	*	*	root	sh /location/SpaceUsed.sh

Example output:
Code:
From Command line
 Space used: 912924
From crontab
 Space used: 1825848

The Code
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 : )_~

Last edited by Festus Hagen; 08-02-2009 at 06:28 PM.. Reason: Clarity
# 5  
Old 08-03-2009
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.
# 6  
Old 08-03-2009
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 ----------

Quote:
Originally Posted by methyl
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 methyl,

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

-Enjoy
fh : )_~
# 7  
Old 08-03-2009
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #544
Difficulty: Medium
Using global variables is generally considered a best practice in modern programming languages..
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate disk space usage email alert

hi all members I have a shell script to generate disk space usage email alert if threshold is more than 80 %, now the requirement changed to keep sending alert emails for every 5% incremental usage ........ Any help would be greatly appreciated. ex - 80% , 85% ,90%,95%,100% we should get an... (6 Replies)
Discussion started by: anil529
6 Replies

2. Shell Programming and Scripting

Need Generic command for disk space usage

Given this directory /web I need to get the current usage (in %) on Linux and Unix both using the same command on bash shell ? The command i tried was working on Unix (solaris) but does not filter the desired same value when run of Linux. My command df -h /web | awk '{print $5}' | sed -n... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. Shell Programming and Scripting

Send Disk Space Usage Status via email

Hi Guys, Is there any way I can write a script that sends DISK SPACE USAGE STATUS via email once a week? Thanks, (5 Replies)
Discussion started by: g4v1n
5 Replies

4. Shell Programming and Scripting

Perl Script to find the disk usage and to delete the files which is consuming more space

Hi All, I have written a script to check the file system usage and to delete the files which is consuming more space.Please check whether the script is corrcet #Script Starts here #!/usr/local/bin/perl #Program to find the disk space and to delete the older files #Checks the type of OS... (8 Replies)
Discussion started by: arunkarthick
8 Replies

5. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

6. UNIX Desktop Questions & Answers

Issue with disk space usage

Issue with disk space usage I have the following line in my "df -h" output: Filesystem Size Used Avail Capacity Mounted on /dev/ad4s1a 496M 495M -39M 109% / What is the issue with having 9% excess utilisation? How can I find out what this partition is... (2 Replies)
Discussion started by: figaro
2 Replies

7. Shell Programming and Scripting

script to monitor disk space usage

Some times my disk space is used upto 100% due to the application logs . So this script is to monitor the disk space usage and wall message to the users about the disk space usage if it exceeds the limit set in the script. Here for example the limit is set to 80%. This job is added in cron to... (2 Replies)
Discussion started by: amitranjansahu
2 Replies

8. UNIX for Dummies Questions & Answers

how to determine the disk space usage

how can we determine the disk space used by a certain directory? (1 Reply)
Discussion started by: gfhgfnhhn
1 Replies

9. Shell Programming and Scripting

How do l test for carriage return & Disk space usage

Hi, I have just written a script in /bin/bash, however, l want to test if character is a carriage return or space. Also l want my script to be able to detect my disk space and send a mail if usage is more than 90% or send an alert. Thanks Kayode (6 Replies)
Discussion started by: kayode
6 Replies

10. UNIX for Dummies Questions & Answers

finding disk space usage

How would I go about finding the about of disk space occupied by a certain directory? For example, /u1/cvera => 530 MB Thanks =) (3 Replies)
Discussion started by: cvera8
3 Replies

Featured Tech Videos