LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-29-2009, 06:42 AM   #1
MartenH
Member
 
Registered: Jul 2005
Location: Lund, Sweden
Distribution: Debian
Posts: 78

Rep: Reputation: 23
Total available/used space of all drives


Hello,

I'm trying to figure out a command which would give me a total sum of space/used space/available space for all my drives.

I've tried playing around with df and df - total but not managed to get it to work.

Would anyone be able to help me out?

Output should be similar to:
Total: 3000 GB
Used: 1111 GB
Available: 1889 GB

PS. A script would work fine as well.
 
Old 03-29-2009, 06:50 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
[redacted]

Last edited by JulianTosh; 03-29-2009 at 06:54 AM. Reason: nevermind/standby
 
Old 03-29-2009, 06:53 AM   #3
MartenH
Member
 
Registered: Jul 2005
Location: Lund, Sweden
Distribution: Debian
Posts: 78

Original Poster
Rep: Reputation: 23
Hi admiral,

Thank you for the answer but that gives me the size for each drive, I'm looking for a total sum for all my drives.
 
Old 03-29-2009, 07:14 AM   #4
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
How about 'parted -l'

Also, dont confuse drives with slices...

'df -h -l' will display the stats on all the slices on all the physical drives currently mounted.
 
Old 03-29-2009, 07:15 AM   #5
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
wow, I suck tonight. that doesnt give you usage.
 
Old 03-29-2009, 07:21 AM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
I made a little diddy that calculates individual drives but, not all drives together.
Code:
#!/usr/bin/perl -w
##### This script calculates hard drive space.
##### example:  ./drive /dev/hda

if($#ARGV != 0){
     die "Example: $0 /dev/sda \n";
}
if(!-e $ARGV[0]){
   die "$ARGV[0] is not a valid device. Exiting.\n";
}

my $DRIVE = $ARGV[0];
print "\n";

open(INFILE, "/proc/partitions") || die "Can't open file for read: $!";
while (<INFILE>){

$DRIVE =~ s/\/\w+\///g;

if ($_ =~ /($DRIVE$)/){
   ($D1, $D2, $D3, $D4, $D5) = split /\s+/;

} elsif ($_ =~ /($DRIVE[0-9])/){
   ($var1, $var2, $var3, $var4, $var5) = split /\s+/;
      if ($var4 == 1){
         print "Partition /dev/$var5   --- Extended ---\n";
      }
      else {
         $totalused = ($totalused += $var4);
         write;
      }
   }
}
$~ = "ENDING";
write;

format STDOUT =
Partition /dev/@<<<< used @######## MB
$var5, ($var4/1000),  $var1,$var2,$var3
.

format ENDING =

Total drive size   @######## MB
($D4/1000),  $D1,$D2,$D3,$D5
Partitioned size   @######## MB
($totalused/1000)
Unpartitioned size @######## MB
(($D4-$totalused)/1000)

.
####End
 
Old 03-29-2009, 07:36 AM   #7
MartenH
Member
 
Registered: Jul 2005
Location: Lund, Sweden
Distribution: Debian
Posts: 78

Original Poster
Rep: Reputation: 23
Thanks both of you. We are getting closer and I really appreciate your help!

Homey: With used/available size I was refering to space used by files. Your script output what has been partitioned.

But it does give me something to build on and adapt
 
Old 03-29-2009, 07:42 AM   #8
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Formatting is a little off, but see if that works for you:

Code:
parted -l | grep '^Disk ' | sed 's/Disk \(.*\):.*/\1/' > /tmp/drives.txt
exec < /tmp/drives.txt
echo "Size Used Avail Device"
while read line
do
  echo -e `df -h $line | cut -c23-38 | grep -v '^Size'` $line
done
 
Old 03-29-2009, 08:03 AM   #9
MartenH
Member
 
Registered: Jul 2005
Location: Lund, Sweden
Distribution: Debian
Posts: 78

Original Poster
Rep: Reputation: 23
I didn't have parted and my sources for urpmi were out of date so it took me a while o get it sorted out. Anyway, the command runs successfully now but the output is not entirely correct. I'm guess the variable doesn't get a new values when expected.

Size Used Avail Device
32M 172K 32M /dev/hda
32M 172K 32M /dev/hdb
32M 172K 32M /dev/hdc
32M 172K 32M /dev/sda
32M 172K 32M /dev/sdb
32M 172K 32M /dev/sdc
32M 172K 32M /dev/sdd
32M 172K 32M /dev/sde
32M 172K 32M /dev/sdf
69G 13G 57G /dev/md1
35G 26G 9,0G /dev/md0
 
Old 03-29-2009, 08:05 AM   #10
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
Hmm. yea, didnt expect that... I dont have multiple drives so my testing was limited. I'll check it out... but that's the correct general direction?
 
Old 03-29-2009, 08:10 AM   #11
MartenH
Member
 
Registered: Jul 2005
Location: Lund, Sweden
Distribution: Debian
Posts: 78

Original Poster
Rep: Reputation: 23
Yes, It's definately the right direction. It seems that df -h /dev/sda gives the wrong values while df -h sda1 works much better.

Btw it doesn't really matter what each drive has, I'm just after a combined sum for all drives. But I guess I can just add a summation variable for that.
 
Old 03-29-2009, 08:14 AM   #12
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
sda1,sda2,sda3 etc are slices on the same drive (sda). So i'm guessing you dont want to know about the individual slices, but rather the combines slice usage on each drive? And the combined total of all drives?
 
Old 03-29-2009, 08:27 AM   #13
MartenH
Member
 
Registered: Jul 2005
Location: Lund, Sweden
Distribution: Debian
Posts: 78

Original Poster
Rep: Reputation: 23
I'm actually only after the combined total of all slices on all drives.

Looking at it a bit closer I'm thinking of just adding up the output from df -h somehow... but not very well acquainted with writing bash scripts. I'd need to parse each line it outputs, split to extract each set of values and sum them together.

I'm looking at using
df | grep 'dev' | awk '{print $4}' | cut -d 'G' -f 1
somehow perhaps...

Last edited by MartenH; 03-29-2009 at 08:36 AM.
 
Old 03-29-2009, 12:27 PM   #14
MartenH
Member
 
Registered: Jul 2005
Location: Lund, Sweden
Distribution: Debian
Posts: 78

Original Poster
Rep: Reputation: 23
I got it working!

The only thing left is to get the decimals for the percentage correct. For some reason $RES simply shows the same as $USEDSIZE

Code:
#!/bin/sh
df -h $line | grep dev | sed 's|/dev/|''|' | awk '{ print $1, $2, $3, $4 }' | sed s/G/''/g > /tmp/drives.txt
exec < /tmp/drives.txt
echo "Device\tSize\tUsed\tAvail"
while read line
do
let FULLSIZE+=`echo -e $line | awk '{ print $2 }'`
let USEDSIZE+=`echo -e $line | awk '{ print $3 }'`
let AVAILSIZE+=`echo -e $line | awk '{ print $4 }'`
   echo -e $line | awk '{ print $1"\t", $2"\t", $3"\t", $4"\t" }'
done

PERCENT=$((100*$USEDSIZE / $FULLSIZE))
let RES=$USEDSIZE%$FULLSIZE

echo Full: $FULLSIZE
echo Used: $USEDSIZE
echo Avail: $AVAILSIZE
echo % used: $PERCENT
echo $RES
 
Old 03-29-2009, 02:11 PM   #15
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
df -m | awk 'NR >1 {total+=$2;used+=$3;avail+=$4}END{print "Total: "total;print "Used: "used;print "Avail: "avail}'

Last edited by Tinkster; 03-29-2009 at 02:12 PM.
 
  


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
How much total Hard Disk Space required to install RHEL AS 5.0 singh_chitranjan Linux - Newbie 3 06-24-2008 01:30 PM
total free space is different than actual sushmi Linux - Newbie 2 06-04-2007 01:48 AM
Nmap help, total newblet from space. zarathaz Linux - Newbie 3 07-19-2005 11:49 PM
format only gives me less than total space ctrl-a Linux - Newbie 4 10-12-2004 04:15 PM
finding total used space by a user in a directory vince_2x Linux - General 1 09-21-2004 06:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:19 AM.

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
Open Source Consulting | Domain Registration