LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-05-2009, 05:18 PM   #1
alienmindvision
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Rep: Reputation: 0
Question trying to add df output columns help


Hello everyone,

First time posting on here and a bit new to Linux and programming so here goes...

I am trying to write a script that will sum up all of the available and free disk space on the local machine running Linux as a bash script. This is what I have so far:

#!/bin/bash

DF='df -h'
NOW=$(date +"%Y-%m-%d_%H:%M:%S")

$DF > diskspace\ $NOW.txt

I have tried using grep to get the totals but I am lost as to how to get the available and Used columns to total up and print into the file created?

Any help would be appreciated.
 
Old 06-05-2009, 07:45 PM   #2
sectionboy
LQ Newbie
 
Registered: Jun 2009
Posts: 7

Rep: Reputation: 1
I am new to shell programming, here is my try. It gives you the used space:

Code:
#!/bin/bash
d1=`df | sed 's/\([/a-z0-9]*\) *[0-9]* *\([0-9]*\).*/\2/'`
usedspace=0
while [ -n "$d1" ]
do
d2=`echo $d1 | sed 's/\([0-9]*\).*/\1/'`
d1=`echo $d1 | sed 's/ *[0-9]*\(.*\)/\1/'`
usedspace=`expr $usedspace + $d2`
done
echo Disk space used: "$usedspace"Byte
 
Old 06-05-2009, 09:04 PM   #3
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Rep: Reputation: 53
Also new to bash but

Code:
df | awk '{sum += $3;} END {print sum;}'
will sum all values in column 3 which is the used output of df

based on that I wrote this

Code:
#!/bin/bash

DF='df -h'
USED=$(df | awk '{sum += $3;} END {print sum;}')
AVAILABLE=$(df | awk '{sum += $4;} END {print sum;}')
NOW=$(date +"%Y-%m-%d_%H:%M:%S")
FILE=diskspace$NOW.txt


$DF > $FILE
echo >> $FILE
echo Available : $AVAILABLE >> $FILE
echo Used : $USED >> $FILE
This gives me this output.

Code:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              19G  6.3G   12G  36% /
tmpfs                1007M     0 1007M   0% /lib/init/rw
varrun               1007M  220K 1006M   1% /var/run
varlock              1007M     0 1007M   0% /var/lock
udev                 1007M  152K 1007M   1% /dev
tmpfs                1007M  216K 1006M   1% /dev/shm
lrm                  1007M  2.4M 1004M   1% /lib/modules/2.6.28-11-generic/volatile
/dev/sda5             191G  9.6G  172G   6% /home

Available : 197655192
Used : 16619324
The two outputs are in KiB (1024 Bytes)

Last edited by archShade; 06-05-2009 at 09:12 PM.
 
Old 06-05-2009, 09:23 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
with use df and awk
Code:
df | awk 'NR>1{
  avail+=$4
  used+=$3  
}END{
  date=strftime("%Y-%m-%d_%H:%M:%s",systime())
  print "available: " avail > "diskspace_"date".txt"
  print "used: "used > "diskspace"date".txt"    
}
'
 
Old 06-06-2009, 12:04 PM   #5
alienmindvision
LQ Newbie
 
Registered: Jun 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by archShade View Post
Also new to bash but

Code:
df | awk '{sum += $3;} END {print sum;}'
will sum all values in column 3 which is the used output of df

based on that I wrote this

Code:
#!/bin/bash

DF='df -h'
USED=$(df | awk '{sum += $3;} END {print sum;}')
AVAILABLE=$(df | awk '{sum += $4;} END {print sum;}')
NOW=$(date +"%Y-%m-%d_%H:%M:%S")
FILE=diskspace$NOW.txt


$DF > $FILE
echo >> $FILE
echo Available : $AVAILABLE >> $FILE
echo Used : $USED >> $FILE
This gives me this output.

Code:
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              19G  6.3G   12G  36% /
tmpfs                1007M     0 1007M   0% /lib/init/rw
varrun               1007M  220K 1006M   1% /var/run
varlock              1007M     0 1007M   0% /var/lock
udev                 1007M  152K 1007M   1% /dev
tmpfs                1007M  216K 1006M   1% /dev/shm
lrm                  1007M  2.4M 1004M   1% /lib/modules/2.6.28-11-generic/volatile
/dev/sda5             191G  9.6G  172G   6% /home

Available : 197655192
Used : 16619324
The two outputs are in KiB (1024 Bytes)
Nice, I was trying to figure out the awk command but my knowledge is weak as to all of those expressions. Thanks everybody for the quick replies! Does anybody know how I could display the output totals as MB and GB or should I just work the math out?
 
Old 06-06-2009, 01:34 PM   #6
archShade
Member
 
Registered: Mar 2006
Location: Delft NL
Distribution: Debian; Slackware; windows 7
Posts: 218

Rep: Reputation: 53
Quote:
Originally Posted by alienmindvision View Post
Does anybody know how I could display the output totals as MB and GB or should I just work the math out?
You could divide the output by 1024 for MiB or (1024)^2 for GiB.

Also thought that possible only calling df once for the sums might be slightly neater and ease expanison but dont think it makes a diffrence.

Maybe somthing like this - like I said I'm new to bash and not sure what is considerd the best/neatest/fastest way.

Code:
#!/bin/bash

#Get values
DFH='df -h'
DFC='df'
USED=$($DFC | awk '{sum += $3;} END {print sum;}')
AVAILABLE=$($DFC | awk '{sum += $4;} END {print sum;}')
NOW=$(date +"%Y-%m-%d_%H:%M:%S")
FILE=diskspace$NOW.txt

#convert to MiB
MUSED=($USED/1024)
MAVAILABLE=($AVAILABLE/1024)

#print to file
$DFH > $FILE
echo >> $FILE
echo Available : $MAVAILABLE M >> $FILE
echo Used : $MUSED M >> $FILE

I don't have a linux box at the moment so cant try it out and my normal way to play with bash scripting is to type in stuff that I think will work then if it doesn't try to fix it, but I can't test.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read large text files (~10GB), parse for columns, output vache Programming 57 04-07-2009 11:51 PM
sed script to read only columns 4 to 6 in output database cranium2004 Programming 10 02-28-2006 08:20 AM
printf: postion cursor to output columns schneidz Programming 5 09-20-2005 09:59 AM
Removing Columns From Output darthtux Programming 4 08-01-2004 10:21 PM
how do I Add multiple columns mysql in postgres Bheki Linux - Software 1 08-28-2003 03:30 PM


All times are GMT -5. The time now is 04:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration