Welcome to Soft32 Linux Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Customized df -h

 
Goto page 1, 2, 3
   Soft32 Home -> Linux -> SUSE RSS
Next:  Bug#540670: goto-fai-progress: FTBFS: dpkg-gencha..  
Author Message
houghi

External


Since: May 12, 2004
Posts: 420



(Msg. 1) Posted: Sun Aug 09, 2009 1:20 pm
Post subject: Customized df -h
Imported from groups: alt>os>linux>suse (more info?)

This message is not archived
Back to top
Login to vote
houghi

External


Since: May 12, 2004
Posts: 420



(Msg. 2) Posted: Sun Aug 09, 2009 3:20 pm
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

This message is not archived
Back to top
Login to vote
Jan Gerrit Kootstra

External


Since: Feb 02, 2006
Posts: 334



(Msg. 3) Posted: Sun Aug 09, 2009 5:20 pm
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

houghi schreef:
> houghi wrote:
>> Oh well. Hope you enjoy it.
>
> A sample output
> #v+
> houghi@penne : ddf
> Filesystem Size Used Avail Use% Mounted on
> udev 4.0G 188K 4.0G 1% /dev
> /dev/sda2 20G 3.5G 16G 19% /media/other
> /dev/sda3 99M 5.7M 89M 7% /media/other/boot
> /dev/sda4 438G 151G 265G 37% /media/backup
> /dev/sdb1 459G 153G 283G 36% /media/movies
> /dev/sdc2 20G 12G 6.7G 65% /
> /dev/sdc3 99M 46M 49M 49% /boot
> /dev/sdc5 9.9G 290M 9.1G 4% /var/spool
> /dev/sdc6 9.9G 1.8G 7.6G 19% /srv
> /dev/sdc7 418G 230G 167G 58% /home
> /dev/sdd1 459G 287G 149G 66% /media/porn
> /dev/sde1 1.4T 198M 1.3T 1% /media/movie_b
> /dev/sdf1 1.4T 390G 916G 30% /media/movie_a
> /dev/sdg1 151G 103G 40G 73% /media/External_170GB
> marschip.TakeThisOut@xs4all.nl: 1000G 0 1000G 0% /home/extra/tmp/xs4all
> root@pizza:/ 73G 53G 17G 77% /home/extra/tmp/pizza
> -------------------------------------------------------------------
> All in GB 5805G 1382G 4178G
> All in TB 5.66T 1.34T 4.08T 23% @penne x86_64
> -------------------------------------------------------------------
> #v-
>
> And with some thing unmounted:
> #v+
> houghi@penne : ddf
> Filesystem Size Used Avail Use% Mounted on
> udev 4.0G 180K 4.0G 1% /dev
> /dev/sda2 20G 3.5G 16G 19% /media/other
> /dev/sda3 99M 5.7M 89M 7% /media/other/boot
> /dev/sda4 438G 151G 265G 37% /media/backup
> /dev/sdb1 459G 153G 283G 36% /media/movies
> /dev/sdc2 20G 12G 6.7G 65% /
> /dev/sdc3 99M 46M 49M 49% /boot
> /dev/sdc5 9.9G 290M 9.1G 4% /var/spool
> /dev/sdc6 9.9G 1.8G 7.6G 19% /srv
> /dev/sdc7 418G 230G 167G 58% /home
> /dev/sdd1 459G 287G 149G 66% /media/porn
> /dev/sde1 1.4T 198M 1.3T 1% /media/movie_b
> /dev/sdf1 1.4T 390G 916G 30% /media/movie_a
> -------------------------------------------------------------------
> All in GB 4582G 1227G 3122G
> All in TB 4.47T 1.19T 3.04T 26% @penne x86_64
> -------------------------------------------------------------------
> #v-
>
> So I still have some place abailable. Very Happy
>
> houghi
Houghi,


There is small misbehavior in the script. If devicenames are long df -h
, and df do line wrapping after the device name.

This can be avoided by using df -hP or df -P instead.

For the rest it is a great extention to df.


Kind regards,


Jan Gerrit Kootstra
P.S. it works on RHEL 5.3 too.
Back to top
Login to vote
J G Miller

External


Since: Sep 04, 2003
Posts: 45



(Msg. 4) Posted: Sun Aug 09, 2009 5:20 pm
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sun, 09 Aug 2009 19:54:41 +0200, houghi wrote:

> I often did a `df -h`, but I never saw how much I had in use and/or
> available in total.

Have you tried discus or pydf?
Back to top
Login to vote
David Bolt

External


Since: Sep 08, 2005
Posts: 188



(Msg. 5) Posted: Sun Aug 09, 2009 9:20 pm
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Sun, 9 Aug 2009, houghi wrote:-

>houghi wrote:
>> Oh well. Hope you enjoy it.
>
>A sample output

<Snip>

My output from the script was a bit wrong because I have an NFS share
mounted:

davjam@moray:~> df-h
donnas.davjam.org:/videos 466G 193G 273G 42% /mounts/donnas/videos
Filesystem Size Used Avail Use% Mounted on
udev 2.0G 392K 2.0G 1% /dev
/dev/mapper/500GB-500_home 410G 253G 158G 62% /home
/dev/mapper/500GB-tmp 9.7G 151M 9.1G 2% /tmp
/dev/mapper/500GB-usr 20G 4.9G 14G 27% /usr
/dev/mapper/500GB-var_log 1.5G 71M 1.4G 5% /var/log
/dev/sda1 160M 16M 137M 11% /boot
/dev/sda5 20G 1.2G 18G 6% /
/dev/sdb1 233G 134G 100G 58% /mnt
/dev/sdc1 96G 62G 35G 64% /local/photographs
/dev/sdc5 96G 23G 74G 24% /local/DVD_temp
/dev/sdc6 95G 37G 53G 42% /local/music
/dev/sdc7 178G 165G 14G 93% /local/ISOs
/dev/sr0 2.1M 2.1M 0 100% /media/SEAGATE
-------------------------------------------------------------------
All in GB 1157G 677G 472G
All in TB 1.12T .66T .46T 58% @moray x86_64
-------------------------------------------------------------------

So, with a "little" tweaking to sort this out, drop the requirement for
a temporary file, and to make some cosmetic changes to the output, I now
get this:

davjam@moray:~> df-h
Filesystem Size Used Avail Use% Mounted on
donnas.davjam.org:/videos 466G 193G 273G 42% /mounts/donnas/videos
udev 2.0G 392K 2.0G 1% /dev
/dev/mapper/500GB-500_home 410G 253G 158G 62% /home
/dev/mapper/500GB-tmp 9.7G 151M 9.1G 2% /tmp
/dev/mapper/500GB-usr 20G 4.9G 14G 27% /usr
/dev/mapper/500GB-var_log 1.5G 71M 1.4G 5% /var/log
/dev/sda1 160M 16M 137M 11% /boot
/dev/sda5 20G 1.2G 18G 6% /
/dev/sdb1 233G 134G 100G 58% /mnt
/dev/sdc1 96G 62G 35G 64% /local/photographs
/dev/sdc5 96G 23G 74G 24% /local/DVD_temp
/dev/sdc6 95G 37G 53G 42% /local/music
/dev/sdc7 178G 165G 14G 93% /local/ISOs
/dev/sr0 2.1M 2.1M 0 100% /media/SEAGATE
-------------------------------------------------------------------
All in GB 1157G 677G 472G
All in TB 1.12T .66T .46T 58% @moray x86_64
-------------------------------------------------------------------

My tweaked script is now this:

#!/bin/bash

# Personalized `df -h` with totals
LINE="-------------------------------------------------------------------"
TMPF=$(df -hP|grep -v "Mounted on")

function max_columns
{
COLS="0"
local STRING
while [ $# -gt 0 ]
do
STRING=$1
COLS=$(( ${#STRING}>${COLS} ? ${#STRING} : ${COLS} ))
shift
done
}

max_columns ${TMPF}

printf "%-${COLS}s\t%8s%8s%8s%8s %-s\n" "Filesystem" "Size" "Used" "Avail" "Use%" "Mounted on"

# cat $TMPF
# Uncomment previous and comment the next two
# if df -h is not shown correctly

function do_columns
{
local DEVICE
local SIZE
local USED
local AVAIL
local PERC
local WHERE

while [ "$#" -gt 0 ]
do
DEVICE=$1 ; shift
SIZE=$1 ; shift
USED=$1 ; shift
AVAIL=$1 ; shift
PERC=$1 ; shift
WHERE=$1 ; shift
printf "%-${COLS}s\t%8s%8s%8s%8s %-s\n" "${DEVICE}" "${SIZE}" "${USED}" "${AVAIL}" "${PERC}" "${WHERE}"
done
}

do_columns $(echo "${TMPF}" | egrep -v '^/|@' |sort)
do_columns $(echo "${TMPF}" | egrep '^/|@' |sort)
# The showing of the total information

TMPF=$(df -P)
echo $LINE

# Get the sizes and percentage
SIZE=$(echo "${TMPF}" | egrep '^\/dev|@' |awk '{t2 = t2 + $2}END{print t2}')
USED=$(echo "${TMPF}" | egrep '^\/dev|@' |awk '{t3 = t3 + $3}END{print t3}')
AVAI=$(echo "${TMPF}" | egrep '^\/dev|@' |awk '{t4 = t4 + $4}END{print t4}')
PERC=$(echo -e "${USED}*100/${SIZE}\nquit"|bc)

# Set in GB
SIZE=$(echo -e "$SIZE/(1024*1024)\nquit"|bc)
USED=$(echo -e "$USED/(1024*1024)\nquit"|bc)
AVAI=$(echo -e "$AVAI/(1024*1024)\nquit"|bc)
printf "%-${COLS}s\t%8s%8s%8s\n" "All in GB" "${SIZE}G" "${USED}G" "${AVAI}G"

#Set in TB
SIZE=$(echo -e "scale=2\n$SIZE/1024\nquit"|bc)
USED=$(echo -e "scale=2\n$USED/1024\nquit"|bc)
AVAI=$(echo -e "scale=2\n$AVAI/1024\nquit"|bc)
printf "%-${COLS}s\t%8s%8s%8s%8s %-s\n" "All in TB" "${SIZE}T" "${USED}T" "${AVAI}T" "${PERC}%" "@$(uname -mn)"
echo ${LINE}


Regards,
David Bolt

--
Team Acorn: http://www.distributed.net/ OGR-NG @ ~100Mnodes RC5-72 @ ~1Mkeys/s
openSUSE 10.3 32b | openSUSE 11.0 32b | |
openSUSE 10.3 64b | openSUSE 11.0 64b | openSUSE 11.1 64b |
RISC OS 3.6 | RISC OS 3.11 | openSUSE 11.1 PPC | TOS 4.02
Back to top
Login to vote
Kevin Nathan

External


Since: Jul 10, 2003
Posts: 56



(Msg. 6) Posted: Thu Aug 13, 2009 1:20 am
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mon, 10 Aug 2009 00:24:06 +0200
houghi <houghi DeleteThis @houghi.org.invalid> wrote:

>-------------------------------------------------------------------
>All in GB 4582G 1226G 3123G
>All in TB 4.47T 1.19T 3.04T 26% @penne x86_64
>-------------------------------------------------------------------
>
>And that was the information I wanted.
>

Recently, I had to learn about Perl's 'format' statement, so I figured
I would use your df problem as a test case. I include it here for you
to do with as you please. Smile Save the text between the lines of equal
signs to a file in a convenient location. If you want to be able to
execute it directly, make sure to make it executable. If you don't want
to do that, just run it with perl:

perl yourscriptname

It is definitely not optimized code -- but I try to write
understandable code instead of obfuscated code so more
programmers can follow it.

Also, I included peta, exa, zetta and yotta conversions for when we
finally get decent sized disks! Smile

Enjoy!

=============================================================

#!/usr/bin/perl

use strict;
use warnings;

# Get the 'df' data:
my @raw = `df --portability --print-type --human-readable`;

# Fix column title with space in it:
$raw[0] =~ s/Mounted\s+on/Mounted_on/gmx;

# Pull off the first line of the data (column names) and put them in array:
my $firstline = shift @raw;
my @title = split '\s+', $firstline;

# Define our multipliers / dividers:
my $kilo = 1024;
my $mega = 1024 * 1024;
my $giga = $mega * 1024;
my $tera = $giga * 1024;
my $peta = $tera * 1024;
my $exa = $peta * 1024; # just for the helluvit!
my $zetta = $exa * 1024; # just for the helluvit!
my $yotta = $zetta * 1024; # just for the helluvit!

# Get lengths for adjustable display table:
my $maxlen1 = 0; # Filesystem max length
my $maxlen2 = 0; # Mounted On max length

my $arrhash = ();

my $totalsize = 0;
my $totalused = 0;
my $totalavail = 0;

# Build a hash from raw array, to reduce use of array subscripting:
foreach my $line (@raw) {
my @tmp = split '\s+', $line;
push @{$arrhash},
{
'filesystem' => $tmp[0],
'type' => $tmp[1],
'size' => $tmp[2],
'used' => $tmp[3],
'avail' => $tmp[4],
'percent' => $tmp[5],
'mounted_on' => $tmp[6],
};

# Total everything up in bytes:
$totalsize += &convert_to_bytes( $tmp[2] ) if $tmp[2];
$totalused += &convert_to_bytes( $tmp[3] ) if $tmp[3];
$totalavail += &convert_to_bytes( $tmp[4] ) if $tmp[4];

# Adjust the max lengths:
$maxlen1 = $maxlen1 < ( length $tmp[0] ) ? length $tmp[0] : $maxlen1;
$maxlen2 = $maxlen2 < ( length $tmp[6] ) ? length $tmp[6] : $maxlen2;
}

print_result( \@title, $arrhash );

exit;

#
# Now for the support routines
#

sub convert_to_bytes {
my ($valstr) = @_;
my $mult = 1;

if ( $valstr =~ m/K/ ) {
$mult = $kilo;
}
elsif ( $valstr =~ m/M/ ) {
$mult = $mega;
}
elsif ( $valstr =~ m/G/ ) {
$mult = $giga;
}
elsif ( $valstr =~ m/T/ ) {
$mult = $tera;
}
elsif ( $valstr =~ m/P/ ) {
$mult = $peta;
}
elsif ( $valstr =~ m/E/ ) {
$mult = $exa;
}
elsif ( $valstr =~ m/Z/ ) {
$mult = $zetta;
}
elsif ( $valstr =~ m/Y/ ) {
$mult = $yotta;
}

# Strip out the non-numerics (K,M,G, etc.)
$valstr =~ s|[^0-9.]||igmx;

# I got some fractional bytes (?) before adding the 'int':
return int( $valstr * $mult );
}

sub print_result {
my ( $title, $arrhash ) = @_;
my $filesystem;
my $size;
my $used;
my $avail;
my $percent;
my $type;
my $mounted_on;

# setup the default format string:
my $format =
"format STDOUT = \n" . '@'
. '<' x ($maxlen1)
. '@>>>>>>> '
. '@>>>>>>> '
. '@>>>>>>> '
. '@>>>>>>> '
. '@>>>>>>> ' . '@'
. '<' x ($maxlen2) . "\n"
. '$filesystem,'
. '$size,'
. '$used,'
. '$avail,'
. '$percent,'
. '$type,'
. '$mounted_on' . "\n.";

# Need to 'eval' it because format strings are interpreted at compile time;
eval $format;
die $@ if $@; # In case there are any errors during eval

# Put our column titles into format variables:
$filesystem = $title[0];
$size = $title[2];
$used = $title[3];
$avail = $title[4];
$percent = $title[5];
$type = $title[1];
$mounted_on = $title[6];

# And then print them:
write;

# Cycle through the actual values, placing them in the format variables:
foreach my $hash ( @{$arrhash} ) {
$filesystem = $hash->{'filesystem'};
$size = $hash->{'size'};
$used = $hash->{'used'};
$avail = $hash->{'avail'};
$percent = $hash->{'percent'};
$type = $hash->{'type'};
$mounted_on = $hash->{'mounted_on'};
write;
}
print "\n"; # Blank line before totals.

no warnings;

my $scalename;
my $amt;

my $format2 =
"format STDOUT = \n"
. '@>>>>>>>>>>>>> '
. '@#############.#### ' . "\n"
. '$scalename,' . '$amt' . "\n.";
eval $format2;
die $@ if $@;

use warnings;

# Should re-design this to use loops (but this was just so easy to do!):
print " Total Size: \n"; # $totalsize ( B)
$scalename = 'KB:';
$amt = round_bytes( $totalsize / $kilo );
write unless $amt < 0.0001;

$scalename = 'MB:';
$amt = round_bytes( $totalsize / $mega );
write unless $amt < 0.0001;

$scalename = 'GB:';
$amt = round_bytes( $totalsize / $giga );
write unless $amt < 0.0001;

$scalename = 'TB:';
$amt = round_bytes( $totalsize / $tera );
write unless $amt < 0.0001;

$scalename = 'PB:';
$amt = round_bytes( $totalsize / $peta );
write unless $amt < 0.0001;

$scalename = 'EB:';
$amt = round_bytes( $totalsize / $exa );
write unless $amt < 0.0001;

$scalename = 'ZB:';
$amt = round_bytes( $totalsize / $zetta );
write unless $amt < 0.0001;

$scalename = 'YB:';
$amt = round_bytes( $totalsize / $yotta );
write unless $amt < 0.0001;

print " Total Used: \n"; # $totalused ( B)
$scalename = 'KB:';
$amt = round_bytes( $totalused / $kilo );
write unless $amt < 0.0001;

$scalename = 'MB:';
$amt = round_bytes( $totalused / $mega );
write unless $amt < 0.0001;

$scalename = 'GB:';
$amt = round_bytes( $totalused / $giga );
write unless $amt < 0.0001;

$scalename = 'TB:';
$amt = round_bytes( $totalused / $tera );
write unless $amt < 0.0001;

$scalename = 'PB:';
$amt = round_bytes( $totalused / $peta );
write unless $amt < 0.0001;

$scalename = 'EB:';
$amt = round_bytes( $totalused / $exa );
write unless $amt < 0.0001;

$scalename = 'ZB:';
$amt = round_bytes( $totalused / $zetta );
write unless $amt < 0.0001;

$scalename = 'YB:';
$amt = round_bytes( $totalused / $yotta );
write unless $amt < 0.0001;

print " Total Avail: \n"; # $totalavail ( B)
$scalename = 'KB:';
$amt = round_bytes( $totalavail / $kilo );
write unless $amt < 0.0001;

$scalename = 'MB:';
$amt = round_bytes( $totalavail / $mega );
write unless $amt < 0.0001;

$scalename = 'GB:';
$amt = round_bytes( $totalavail / $giga );
write unless $amt < 0.0001;

$scalename = 'TB:';
$amt = round_bytes( $totalavail / $tera );
write unless $amt < 0.0001;

$scalename = 'PB:';
$amt = round_bytes( $totalavail / $peta );
write unless $amt < 0.0001;

$scalename = 'EB:';
$amt = round_bytes( $totalavail / $exa );
write unless $amt < 0.0001;

$scalename = 'ZB:';
$amt = round_bytes( $totalavail / $zetta );
write unless $amt < 0.0001;

$scalename = 'YB:';
$amt = round_bytes( $totalavail / $yotta );
write unless $amt < 0.0001;
}

use POSIX;

sub round_bytes {
my ( $raw, $places ) = @_;
return 0 unless $raw; # Must have a value on which to work!
$places = 4 unless $places; # Provide default, just in case
my $scale = 10**$places;

return POSIX::floor( ( $raw * $scale ) + 0.5 ) / $scale;
}

=============================================================



--
Kevin Nathan (Arizona, USA)
Linux Potpourri and a.o.l.s. FAQ -- (temporarily offline)

Open standards. Open source. Open minds.
The command line is the front line.
Linux 2.6.25.20-0.4-pae
1:07am up 25 days 2:29, 35 users, load average: 0.32, 0.28, 0.27
Back to top
Login to vote
houghi

External


Since: May 12, 2004
Posts: 420



(Msg. 7) Posted: Thu Aug 13, 2009 5:20 am
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

This message is not archived
Back to top
Login to vote
David Bolt

External


Since: Sep 08, 2005
Posts: 188



(Msg. 8) Posted: Thu Aug 13, 2009 7:20 am
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 13 Aug 2009, houghi wrote:-

>I just can't figure out how to do the right alighnement in bash.

Use printf. For a string, using a number after the % will specify the
minimum number of characters allowed for that string, and the string
will be printed right-aligned. Adding a '-' between the % and the number
will result in the string being printed left-align. You can do the same
for numbers and, by including a '0' between the % and the number of
desired columns, you can zero-pad the output:

davjam@moray:~/articles> printf "%-10s\n" "test"
test
davjam@moray:~/articles> printf "%10s\n" "test"
test
davjam@moray:~/articles> printf "%10i\n" 10
10
davjam@moray:~/articles> printf "%010i\n" 10
0000000010
davjam@moray:~/articles> printf "%-10i\n" 10
10
davjam@moray:~/articles> printf "%-010i\n" 10
10

Another useful thing I've learnt recently is that you can tell printf to
assign the output to a variable by including the option -v :

davjam@moray:~/articles> unset i
davjam@moray:~/articles> echo "${i}"

davjam@moray:~/articles> printf -v i "%s" "test"
davjam@moray:~/articles> echo "${i}"
test



Regards,
David Bolt

--
Team Acorn: http://www.distributed.net/ OGR-NG @ ~100Mnodes RC5-72 @ ~1Mkeys/s
openSUSE 10.3 32b | openSUSE 11.0 32b | |
openSUSE 10.3 64b | openSUSE 11.0 64b | openSUSE 11.1 64b |
RISC OS 3.6 | RISC OS 3.11 | openSUSE 11.1 PPC | TOS 4.02
Back to top
Login to vote
Kevin Nathan

External


Since: Jul 10, 2003
Posts: 56



(Msg. 9) Posted: Thu Aug 13, 2009 7:59 am
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 13 Aug 2009 11:23:46 +0200
houghi <houghi RemoveThis @houghi.org.invalid> wrote:

>Neat. I like the first part. The second part does not look so nice.

Yes, I know, but it was the easiest way to do it -- especially since I
was using this as a way to understand Perl's 'format/write' statements.


>I would prefere something that uses the same columns as what it does
>above. That way you have one view instead of clearly two different
>things in one output.
>

So would I -- I will look at fixing that up, as a further exercise.


>Oh, and there is no sort on the first part. I am looking at it to read
>/media/backup 438G 164G 253G 40% /dev/sda4
>/media/movies 459G 167G 269G 39% /dev/sdb1
>
>instead of
>/dev/sda4 438G 164G 253G 40% /media/backup
>/dev/sdb1 459G 167G 269G 39% /media/movies
>

The sort will be easy. I'll work on that, too.


>I just can't figure out how to do the right alighnement in bash.
>

David gave you a good answer on that one . . .


--
Kevin Nathan (Arizona, USA)
Linux Potpourri and a.o.l.s. FAQ -- (temporarily offline)

Open standards. Open source. Open minds.
The command line is the front line.
Linux 2.6.25.20-0.4-pae
7:56am up 25 days 9:18, 35 users, load average: 0.76, 1.14, 1.09
Back to top
Login to vote
Kevin Nathan

External


Since: Jul 10, 2003
Posts: 56



(Msg. 10) Posted: Thu Aug 13, 2009 8:37 am
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 13 Aug 2009 11:23:46 +0200
houghi <houghi.TakeThisOut@houghi.org.invalid> wrote:

>Oh, and there is no sort on the first part. I am looking at it to read
>/media/backup 438G 164G 253G 40% /dev/sda4
>/media/movies 459G 167G 269G 39% /dev/sdb1
>
>instead of
>/dev/sda4 438G 164G 253G 40% /media/backup
>/dev/sdb1 459G 167G 269G 39% /media/movies
>

Here's how to do the sorting and change the output to match your's,
above.

In the 'print_result' function, make the default format string look like
this:

# setup the default format string:
my $format =
"format STDOUT = \n" . '@'
. '<' x ($maxlen2)
. '@>>>>>>> '
. '@>>>>>>> '
. '@>>>>>>> '
. '@>>>>>>> '
. '@>>>>>>> ' . '@'
. '<' x ($maxlen1) . "\n"
. '$mounted_on,'
. '$size,'
. '$used,'
. '$avail,'
. '$percent,'
. '$type,'
. '$filesystem' . "\n.";


and for the '# Cycle through the actual values', make it look like this:

# First, sort the array of hashes based on specified hash key:
my @sorted =
sort { $$a{mounted_on} cmp $$b{mounted_on} } @{$arrhash};
# Cycle through the actual values,
# placing them in the format variables:
foreach my $hash ( @sorted ) {
$filesystem = $hash->{'filesystem'};
$size = $hash->{'size'};
$used = $hash->{'used'};
$avail = $hash->{'avail'};
$percent = $hash->{'percent'};
$type = $hash->{'type'};
$mounted_on = $hash->{'mounted_on'};
write;
}
print "\n"; # Blank line before totals.


At some point, I would like to make the sorted by key be selectable by
user, as well as the order of columns -- but that is for another day.

Tonight I will try cleaning up the totals section . . .

System programming is still *way* more fun than web programming! Wink


--
Kevin Nathan (Arizona, USA)
Linux Potpourri and a.o.l.s. FAQ -- (temporarily offline)

Open standards. Open source. Open minds.
The command line is the front line.
Linux 2.6.25.20-0.4-pae
8:26am up 25 days 9:48, 35 users, load average: 0.21, 0.22, 0.34
Back to top
Login to vote
Kevin Nathan

External


Since: Jul 10, 2003
Posts: 56



(Msg. 11) Posted: Thu Aug 13, 2009 9:07 am
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 13 Aug 2009 17:49:24 +0200
Jan Gerrit Kootstra <jan.gerrit.DeleteThis@kootstra.org.uk> wrote:

>I do not understand perl, so I am unable to find the error.
>
>The script is able to find 237GB more total size,
>then the total of physical disk space.
>

Send me the output of:

df --portability --print-type --human-readable

and I will look into it as soon as I can. That's a pretty serious
difference! Smile


--
Kevin Nathan (Arizona, USA)
Linux Potpourri and a.o.l.s. FAQ -- (temporarily offline)

Open standards. Open source. Open minds.
The command line is the front line.
Linux 2.6.25.20-0.4-pae
9:04am up 25 days 10:26, 35 users, load average: 0.45, 0.54, 0.49
Back to top
Login to vote
Jan Gerrit Kootstra

External


Since: Feb 02, 2006
Posts: 334



(Msg. 12) Posted: Thu Aug 13, 2009 11:20 am
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Kevin Nathan schreef:
> On Mon, 10 Aug 2009 00:24:06 +0200
> houghi <houghi.DeleteThis@houghi.org.invalid> wrote:
>
>> -------------------------------------------------------------------
>> All in GB 4582G 1226G 3123G
>> All in TB 4.47T 1.19T 3.04T 26% @penne x86_64
>> -------------------------------------------------------------------
>>
>> And that was the information I wanted.
>>
>
> Recently, I had to learn about Perl's 'format' statement, so I figured
> I would use your df problem as a test case. I include it here for you
> to do with as you please. Smile Save the text between the lines of equal
> signs to a file in a convenient location. If you want to be able to
> execute it directly, make sure to make it executable. If you don't want
> to do that, just run it with perl:
>
> perl yourscriptname
>
> It is definitely not optimized code -- but I try to write
> understandable code instead of obfuscated code so more
> programmers can follow it.
>
> Also, I included peta, exa, zetta and yotta conversions for when we
> finally get decent sized disks! Smile
>
> Enjoy!
>
> =============================================================
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> # Get the 'df' data:
> my @raw = `df --portability --print-type --human-readable`;
>
> # Fix column title with space in it:
> $raw[0] =~ s/Mounted\s+on/Mounted_on/gmx;
>
> # Pull off the first line of the data (column names) and put them in array:
> my $firstline = shift @raw;
> my @title = split '\s+', $firstline;
>
> # Define our multipliers / dividers:
> my $kilo = 1024;
> my $mega = 1024 * 1024;
> my $giga = $mega * 1024;
> my $tera = $giga * 1024;
> my $peta = $tera * 1024;
> my $exa = $peta * 1024; # just for the helluvit!
> my $zetta = $exa * 1024; # just for the helluvit!
> my $yotta = $zetta * 1024; # just for the helluvit!
>
> # Get lengths for adjustable display table:
> my $maxlen1 = 0; # Filesystem max length
> my $maxlen2 = 0; # Mounted On max length
>
> my $arrhash = ();
>
> my $totalsize = 0;
> my $totalused = 0;
> my $totalavail = 0;
>
> # Build a hash from raw array, to reduce use of array subscripting:
> foreach my $line (@raw) {
> my @tmp = split '\s+', $line;
> push @{$arrhash},
> {
> 'filesystem' => $tmp[0],
> 'type' => $tmp[1],
> 'size' => $tmp[2],
> 'used' => $tmp[3],
> 'avail' => $tmp[4],
> 'percent' => $tmp[5],
> 'mounted_on' => $tmp[6],
> };
>
> # Total everything up in bytes:
> $totalsize += &convert_to_bytes( $tmp[2] ) if $tmp[2];
> $totalused += &convert_to_bytes( $tmp[3] ) if $tmp[3];
> $totalavail += &convert_to_bytes( $tmp[4] ) if $tmp[4];
>
> # Adjust the max lengths:
> $maxlen1 = $maxlen1 < ( length $tmp[0] ) ? length $tmp[0] : $maxlen1;
> $maxlen2 = $maxlen2 < ( length $tmp[6] ) ? length $tmp[6] : $maxlen2;
> }
>
> print_result( \@title, $arrhash );
>
> exit;
>
> #
> # Now for the support routines
> #
>
> sub convert_to_bytes {
> my ($valstr) = @_;
> my $mult = 1;
>
> if ( $valstr =~ m/K/ ) {
> $mult = $kilo;
> }
> elsif ( $valstr =~ m/M/ ) {
> $mult = $mega;
> }
> elsif ( $valstr =~ m/G/ ) {
> $mult = $giga;
> }
> elsif ( $valstr =~ m/T/ ) {
> $mult = $tera;
> }
> elsif ( $valstr =~ m/P/ ) {
> $mult = $peta;
> }
> elsif ( $valstr =~ m/E/ ) {
> $mult = $exa;
> }
> elsif ( $valstr =~ m/Z/ ) {
> $mult = $zetta;
> }
> elsif ( $valstr =~ m/Y/ ) {
> $mult = $yotta;
> }
>
> # Strip out the non-numerics (K,M,G, etc.)
> $valstr =~ s|[^0-9.]||igmx;
>
> # I got some fractional bytes (?) before adding the 'int':
> return int( $valstr * $mult );
> }
>
> sub print_result {
> my ( $title, $arrhash ) = @_;
> my $filesystem;
> my $size;
> my $used;
> my $avail;
> my $percent;
> my $type;
> my $mounted_on;
>
> # setup the default format string:
> my $format =
> "format STDOUT = \n" . '@'
> . '<' x ($maxlen1)
> . '@>>>>>>> '
> . '@>>>>>>> '
> . '@>>>>>>> '
> . '@>>>>>>> '
> . '@>>>>>>> ' . '@'
> . '<' x ($maxlen2) . "\n"
> . '$filesystem,'
> . '$size,'
> . '$used,'
> . '$avail,'
> . '$percent,'
> . '$type,'
> . '$mounted_on' . "\n.";
>
> # Need to 'eval' it because format strings are interpreted at compile time;
> eval $format;
> die $@ if $@; # In case there are any errors during eval
>
> # Put our column titles into format variables:
> $filesystem = $title[0];
> $size = $title[2];
> $used = $title[3];
> $avail = $title[4];
> $percent = $title[5];
> $type = $title[1];
> $mounted_on = $title[6];
>
> # And then print them:
> write;
>
> # Cycle through the actual values, placing them in the format variables:
> foreach my $hash ( @{$arrhash} ) {
> $filesystem = $hash->{'filesystem'};
> $size = $hash->{'size'};
> $used = $hash->{'used'};
> $avail = $hash->{'avail'};
> $percent = $hash->{'percent'};
> $type = $hash->{'type'};
> $mounted_on = $hash->{'mounted_on'};
> write;
> }
> print "\n"; # Blank line before totals.
>
> no warnings;
>
> my $scalename;
> my $amt;
>
> my $format2 =
> "format STDOUT = \n"
> . '@>>>>>>>>>>>>> '
> . '@#############.#### ' . "\n"
> . '$scalename,' . '$amt' . "\n.";
> eval $format2;
> die $@ if $@;
>
> use warnings;
>
> # Should re-design this to use loops (but this was just so easy to do!):
> print " Total Size: \n"; # $totalsize ( B)
> $scalename = 'KB:';
> $amt = round_bytes( $totalsize / $kilo );
> write unless $amt < 0.0001;
>
> $scalename = 'MB:';
> $amt = round_bytes( $totalsize / $mega );
> write unless $amt < 0.0001;
>
> $scalename = 'GB:';
> $amt = round_bytes( $totalsize / $giga );
> write unless $amt < 0.0001;
>
> $scalename = 'TB:';
> $amt = round_bytes( $totalsize / $tera );
> write unless $amt < 0.0001;
>
> $scalename = 'PB:';
> $amt = round_bytes( $totalsize / $peta );
> write unless $amt < 0.0001;
>
> $scalename = 'EB:';
> $amt = round_bytes( $totalsize / $exa );
> write unless $amt < 0.0001;
>
> $scalename = 'ZB:';
> $amt = round_bytes( $totalsize / $zetta );
> write unless $amt < 0.0001;
>
> $scalename = 'YB:';
> $amt = round_bytes( $totalsize / $yotta );
> write unless $amt < 0.0001;
>
> print " Total Used: \n"; # $totalused ( B)
> $scalename = 'KB:';
> $amt = round_bytes( $totalused / $kilo );
> write unless $amt < 0.0001;
>
> $scalename = 'MB:';
> $amt = round_bytes( $totalused / $mega );
> write unless $amt < 0.0001;
>
> $scalename = 'GB:';
> $amt = round_bytes( $totalused / $giga );
> write unless $amt < 0.0001;
>
> $scalename = 'TB:';
> $amt = round_bytes( $totalused / $tera );
> write unless $amt < 0.0001;
>
> $scalename = 'PB:';
> $amt = round_bytes( $totalused / $peta );
> write unless $amt < 0.0001;
>
> $scalename = 'EB:';
> $amt = round_bytes( $totalused / $exa );
> write unless $amt < 0.0001;
>
> $scalename = 'ZB:';
> $amt = round_bytes( $totalused / $zetta );
> write unless $amt < 0.0001;
>
> $scalename = 'YB:';
> $amt = round_bytes( $totalused / $yotta );
> write unless $amt < 0.0001;
>
> print " Total Avail: \n"; # $totalavail ( B)
> $scalename = 'KB:';
> $amt = round_bytes( $totalavail / $kilo );
> write unless $amt < 0.0001;
>
> $scalename = 'MB:';
> $amt = round_bytes( $totalavail / $mega );
> write unless $amt < 0.0001;
>
> $scalename = 'GB:';
> $amt = round_bytes( $totalavail / $giga );
> write unless $amt < 0.0001;
>
> $scalename = 'TB:';
> $amt = round_bytes( $totalavail / $tera );
> write unless $amt < 0.0001;
>
> $scalename = 'PB:';
> $amt = round_bytes( $totalavail / $peta );
> write unless $amt < 0.0001;
>
> $scalename = 'EB:';
> $amt = round_bytes( $totalavail / $exa );
> write unless $amt < 0.0001;
>
> $scalename = 'ZB:';
> $amt = round_bytes( $totalavail / $zetta );
> write unless $amt < 0.0001;
>
> $scalename = 'YB:';
> $amt = round_bytes( $totalavail / $yotta );
> write unless $amt < 0.0001;
> }
>
> use POSIX;
>
> sub round_bytes {
> my ( $raw, $places ) = @_;
> return 0 unless $raw; # Must have a value on which to work!
> $places = 4 unless $places; # Provide default, just in case
> my $scale = 10**$places;
>
> return POSIX::floor( ( $raw * $scale ) + 0.5 ) / $scale;
> }
>
> =============================================================
>
>
>
Kevin,


I do not understand perl, so I am unable to find the error.

The script is able to find 237GB more total size,
then the total of physical disk space.

=========================================================================
fdisk -l

Schijf /dev/hda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Eenheden = cylinders van 16065 * 512 = 8225280 bytes

Apparaat Boot Start Einde Blokken Id Systeem
/dev/hda1 1 63 506016 83 Linux
/dev/hda2 251 30401 242187907+ 8e Linux LVM
/dev/hda3 64 250 1502077+ 8e Linux LVM

Partitietabel ingangen zijn niet in schijfvolgorde

Schijf /dev/sda: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Eenheden = cylinders van 16065 * 512 = 8225280 bytes

Apparaat Boot Start Einde Blokken Id Systeem
/dev/sda1 * 1 30401 244196001 8e Linux LVM

Schijf /dev/sdb: 250.0 GB, 250059350016 bytes
255 heads, 63 sectors/track, 30401 cylinders
Eenheden = cylinders van 16065 * 512 = 8225280 bytes

Apparaat Boot Start Einde Blokken Id Systeem
/dev/sdb1 * 1 30401 244196001 8e Linux LVM

==========================================================================

df_perl.pl
Bestandssysteem Grtte Gebr Besch Geb%
Type Aangekoppeld
/dev/mapper/VolGroup00-LogVol00 9,7G 4,3G 5,0G 47%
ext3 /
/dev/hda1 479M 46M 409M 11%
ext3 /boot
tmpfs 2,9G 0 2,9G 0%
tmpfs /dev/shm
/dev/mapper/VolGroup00-LogVol01 4,0G 53M 3,7G 2%
ext3 /home
/dev/mapper/VolGroup00-LogVol02 2,0G 144M 1,7G 8%
ext3 /tmp
/dev/mapper/VolGroup00-LogVol03 61G 21G 37G 37%
ext3 /usr
/dev/mapper/VolGroup00-LogVol04 7,8G 3,1G 4,3G 43%
ext3 /var
/dev/mapper/VolGroup00-LogVol05 1008M 150M 808M 16%
ext3 /usr/local
/dev/mapper/VolGroup00-LogVol06 13G 1,8G 11G 15%
ext3 /opt
/dev/mapper/VolGroup00-LogVol07 15G 11G 3,9G 73%
ext3 /tools
/dev/mapper/VolGroup00-LogVol08 39G 15G 23G 40%
ext3 /s01
/dev/mapper/VolGroup00-LogVol09 473G 263G 210G 56%
ext3 /project
/dev/mapper/VolGroup00-LogVol11 35G 31G 3,1G 91%
ext3 /var/www
/dev/mapper/VolGroup00-LogVol12 3,0G 967M 1,9G 34%
ext3 /xcdroast
/dev/mapper/VolGroup00-LogVol13 1008M 101M 887M 11%
ext3 /var/cache/yum
/dev/mapper/VolGroup00-LogVol14 496M 19M 458M 4%
ext3 /var/ntop
/dev/mapper/VolGroup00-LogVol15 5,5G 343M 5,0G 7%
ext3 /var/log

Total Size:
KB: 1035910144.0000
MB: 1011631.0000
GB: 987.9209
TB: 0.9648
PB: 0.0009
Total Used:
KB: 455900160.0000
MB: 445215.0000
GB: 434.7803
TB: 0.4246
PB: 0.0004
Total Avail:
KB: 627574784.0000
MB: 612866.0000
GB: 598.5020
TB: 0.5845
PB: 0.0006


Kind regards,


Jan Gerrit Kootstra
Back to top
Login to vote
Kevin Nathan

External


Since: Jul 10, 2003
Posts: 56



(Msg. 13) Posted: Thu Aug 13, 2009 12:38 pm
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 13 Aug 2009 18:41:46 +0200
Jan Gerrit Kootstra <jan.gerrit.DeleteThis@kootstra.org.uk> wrote:

>I would like to have this 'ghost disk'
>

Yes, wouldn't that be nice? Smile

Thanks for the data, I will look at that after work . . .


--
Kevin Nathan (Arizona, USA)
Linux Potpourri and a.o.l.s. FAQ -- (temporarily offline)

Open standards. Open source. Open minds.
The command line is the front line.
Linux 2.6.25.20-0.4-pae
12:37pm up 25 days 13:59, 35 users, load average: 0.18, 0.28, 0.36
Back to top
Login to vote
Kevin Nathan

External


Since: Jul 10, 2003
Posts: 56



(Msg. 14) Posted: Thu Aug 13, 2009 12:41 pm
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Thu, 13 Aug 2009 18:49:32 +0200
Jan Gerrit Kootstra <jan.gerrit.DeleteThis@kootstra.org.uk> wrote:

>Probably a localisation error. I use LANG=nl_NL.UTF-8
>
>If I export LANG=C before running the script all is well.
>

Yes, it's because one of the column names (Mounted on) has a space in
it and wrote it to replace that with underscore. There is no easy way
to do it like that in all languages so I will modify the code to ignore
the final text ('on' for me, 'op' for you) and hope that this column
has a space in it in all languages. That should at least get it a
little closer . . . Smile


--
Kevin Nathan (Arizona, USA)
Linux Potpourri and a.o.l.s. FAQ -- (temporarily offline)

Open standards. Open source. Open minds.
The command line is the front line.
Linux 2.6.25.20-0.4-pae
12:38pm up 25 days 14:00, 35 users, load average: 0.21, 0.27, 0.35
Back to top
Login to vote
Jan Gerrit Kootstra

External


Since: Feb 02, 2006
Posts: 334



(Msg. 15) Posted: Thu Aug 13, 2009 1:20 pm
Post subject: Re: Customized df -h [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Kevin Nathan schreef:
> On Thu, 13 Aug 2009 17:49:24 +0200
> Jan Gerrit Kootstra <jan.gerrit.TakeThisOut@kootstra.org.uk> wrote:
>
>> I do not understand perl, so I am unable to find the error.
>>
>> The script is able to find 237GB more total size,
>> then the total of physical disk space.
>>
>
> Send me the output of:
>
> df --portability --print-type --human-readable
>
> and I will look into it as soon as I can. That's a pretty serious
> difference! Smile
>
>
df --portability --print-type --human-readable
Bestandssysteem Type Grtte Gebr Besch Geb% Aangekoppeld op
/dev/mapper/VolGroup00-LogVol00 ext3 9,7G 4,3G 5,0G 47% /
/dev/hda1 ext3 479M 46M 409M 11% /boot
tmpfs tmpfs 2,9G 0 2,9G 0% /dev/shm
/dev/mapper/VolGroup00-LogVol01 ext3 4,0G 53M 3,7G 2% /home
/dev/mapper/VolGroup00-LogVol02 ext3 2,0G 144M 1,7G 8% /tmp
/dev/mapper/VolGroup00-LogVol03 ext3 61G 21G 37G 37% /usr
/dev/mapper/VolGroup00-LogVol04 ext3 7,8G 3,1G 4,3G 43% /var
/dev/mapper/VolGroup00-LogVol05 ext3 1008M 150M 808M 16% /usr/local
/dev/mapper/VolGroup00-LogVol06 ext3 13G 1,8G 11G 15% /opt
/dev/mapper/VolGroup00-LogVol07 ext3 15G 11G 3,9G 73% /tools
/dev/mapper/VolGroup00-LogVol08 ext3 39G 15G 23G 40% /s01
/dev/mapper/VolGroup00-LogVol09 ext3 473G 263G 210G 56% /project
/dev/mapper/VolGroup00-LogVol11 ext3 35G 31G 3,1G 91% /var/www
/dev/mapper/VolGroup00-LogVol12 ext3 3,0G 967M 1,9G 34% /xcdroast
/dev/mapper/VolGroup00-LogVol13 ext3 1008M 101M 887M 11% /var/cache/yum
/dev/mapper/VolGroup00-LogVol14 ext3 496M 19M 458M 4% /var/ntop
/dev/mapper/VolGroup00-LogVol15 ext3 5,5G 343M 5,0G 7% /var/log

I would like to have this 'ghost disk'


Regards,


Jan Gerrit
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Surely suse is not this popular - At time of posting and accorind to torrentspy.com there are over 185437 people downloading Suse! No way! ..

YAST - Change Location Problem - Having problem with YAST (SLES9) where the original source of installation was deleted via 'Change Source of..

FTP - I have installed vsftpd but I am wondering how I set it up so that FTPing requires a password? The docs do not seem to....

pro100 - McDoo wrote: ..> I just purchased the pro/1000 gt nic. Suse 10.2 does not seem to have a >> diver for them....

openSuSE 10.2 No virtual consoles at RunLevel 5? - Hi, a.o.l.s Just migrated to 10.2 (from 9.3). Most stuff seems to be working, but just noticed that I can't use the..

System resolution timer too low - When I try to start the Rosegarden4 MIDI sequencer it trows up a box that says my system timer resolution is too low an...
       Soft32 Home -> Linux -> SUSE All times are: Pacific Time (US & Canada) (change)
Goto page 1, 2, 3
Page 1 of 3

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Categories:
 Windows
  Linux
 Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]