Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: disk space utilization

by graff (Chancellor)
on Apr 09, 2005 at 05:16 UTC ( #446221=note: print w/replies, xml ) Need Help??

in reply to Re: disk space utilization
in thread disk space utilization

Perl's "-a" command-line option allows you to pretend that you're using something that resembles "awk" (that's why it's "-a") -- differences between "perl -a" and "awk" are that perl uses @F where awk uses $1,$2,..., and the first token on the line is $F[0] instead of $1:
df -k | perl -lane 'print $F[3]'
(the "-l" option helps make line-feed handling more like awk as well). I presume this still doesn't do exactly what the OP intended, since it prints a line of output for every line of input from "df -k"; but the OP's use of "grep '/^' " seems odd -- not sure what's intended by that.

To limit the output to a particular line, one can cite the target path as an arg to "df", and/or use a condition in the perl script:

df -k / | perl -lane 'print $F[3] if /\d/' # skips column headings # or just: df -k | perl -lane 'print $F[3] if m{/$}' # only prints value for "/ +"

Replies are listed 'Best First'.
Re^3: disk space utilization
by ministry (Scribe) on Apr 12, 2005 at 15:49 UTC
    Thanks for the info graff, the use of an expression grep "/^" in the middle of my arguement will only report back the line of output in the df -k that has a slash that is immediately followed by an end of line tag (anchor)- thereby only returning the root partition, as opposed to only doing a basic grep for a slash, which will return every slice starting with a slash (working in ksh).
    Is there any way I can incorporate your 'one-liner' into my script? As this is only one function out of several that needs to be performed.

    Good judgement comes with experience. Unfortunately, the experience usually comes from bad judgement.
      If what you want is to get the available space on a specific disk volume from within a perl script, I'd do something like this:
      my $targ_vol = "/"; my @df_output = `df -k $targ_vol`; # output should be two lines: # first line is column headings, second line is data my %df_data; if ( @df_output ) { my @headings = split( /\s+/, $df_output[0] ); my @data = split( /\s+/, $df_output[1] ); @df_data{@headings} = @data; } print "available on $targ_vol: $df_data{Avail}\n"; # (your version of df might use some string similar to but different f +rom "Avail")
      You shouldn't have a problem if you're just looking at "/".

      (update: if you're looking at some path other than "/", you need to be careful about knowing whether it's on some different file system, and figuring out whether that file system is currently mounted -- if it isn't, df will give you info about "/" instead. And God help you if it's mounted as NFS, and the NFS server has gone down...)

      (update: BTW, when I try "df | egrep '/^'", nothing comes out, which is what I would expect; but if I do "df | egrep '/$'", I get the line that ends with a slash. That's what I don't understand about your usage in the OP -- I think it can't work as posted, unless your "egrep" is way different from mine...)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://446221]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2023-09-24 19:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found