Portable unix du command

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Portable unix du command

    Howdy group.

    I have a need to get the size of a directory.
    I could just execute unix's du command (my $sz = `du -sh /usr`;)
    but was wondering if there's a better way to do it that is more portable.
    I looked on cpan but didn't see anythign that jumped out at me.

    TIA

    Dan

    Dan Muey Guest

  2. Similar Questions and Discussions

    1. Wrapping Unix Command into Perl
      I am trying to wrap the following Unix command into perl and having a few issues: find /var/spool/Tivoli/backups -name "DB_*" -mtime +10 -print...
    2. I need help on a command for my Unix class---PLEASE ! ?
      I've got a file that contains one line of text. The line looks like this: 34 That's all. When I try to echo the results in a sentence,...
    3. what is different between SIZE and RES of unix command top?
      Hi there, i saw the man page for top command SIZE : the total size of the process(text, data, and stack) RES : the current amount of resident...
    4. HELP NEEDED WITH UNIX COMMAND MAN
      HI Hey i have a quick question. How do we redirect the output of the man command to file which does not have junk or control charaters. i think...
    5. Portable Unix utility scripts
      The Typhoon Toolkit (http://www.raycosoft.com/rayco/products/typhoon) contains about 90 portable utility programs for text processing, file and...
  3. #2

    Default Re: Portable unix du command


    On Tuesday, Nov 18, 2003, at 09:20 US/Pacific, Dan Muey wrote:
    > Howdy group.
    >
    > I have a need to get the size of a directory.
    > I could just execute unix's du command (my $sz = `du -sh /usr`;)
    > but was wondering if there's a better way to do it that is more
    > portable.
    > I looked on cpan but didn't see anythign that jumped out at me.
    >
    a part of the problem is that the 'du' command
    is basically implemented differently between the
    BSD and SYSV styles of unix. One strategy that
    I have adopted when I am looking at a 'common'(HA!)
    unix utility that I would like to use, and
    in your illustration, the 'flags' to it, that
    are problematic, I create a wrapper class.

    a quicky way to think about your problem would
    be my WhackJob for dealing with my 'favorite'
    set of flags to the 'ps' command:

    cf
    <http://www.wetware.com/drieux/CS/Proj/PID/>

    To be honest, I use something like that, but more
    cleaned up in a project I have. The trick of
    course is getting 'illustrations' from all of the
    OS's that you will want to be 'portable' with.

    HTH.

    ciao
    drieux

    ---

    Drieux Guest

  4. #3

    Default RE: Portable unix du command

    > On Tuesday, Nov 18, 2003, at 09:20 US/Pacific, Dan Muey wrote:
    >
    > > Howdy group.
    > >
    > > I have a need to get the size of a directory.
    > > I could just execute unix's du command (my $sz = `du -sh
    > /usr`;) but
    > > was wondering if there's a better way to do it that is more
    > portable.
    > > I looked on cpan but didn't see anythign that jumped out at me.
    > >
    >
    > a part of the problem is that the 'du' command
    > is basically implemented differently between the
    > BSD and SYSV styles of unix. One strategy that
    > I have adopted when I am looking at a 'common'(HA!)
    > unix utility that I would like to use, and
    > in your illustration, the 'flags' to it, that
    > are problematic, I create a wrapper class.
    >
    > a quicky way to think about your problem would
    > be my WhackJob for dealing with my 'favorite'
    > set of flags to the 'ps' command:
    >
    > cf
    > <http://www.wetware.com/drieux/CS/Proj/PID/>
    >
    > To be honest, I use something like that, but more
    > cleaned up in a project I have. The trick of
    > course is getting 'illustrations' from all of the
    > OS's that you will want to be 'portable' with.
    >
    Thanks. What I really want to do is avoid system commands completely.
    I could always traverse the directory adding the size of each file
    together and do that all in Perl with no system commands but I figured
    there'd already be a Module or something like this, so I want to do it
    that way. Somthign like:

    my $sz = $module->size("/home/joemama");
    # return sixze of file or of entire directory if specified file is a directory
    > HTH.
    >
    > ciao
    > drieux
    Dan Muey Guest

  5. #4

    Default Re: Portable unix du command

    Dan Muey wrote:
    >
    > Thanks. What I really want to do is avoid system commands completely.
    > I could always traverse the directory adding the size of each file
    > together and do that all in Perl with no system commands but I figured
    > there'd already be a Module or something like this, so I want to do it
    > that way. Somthign like:
    You mean something like (untested):

    use File::Find;

    my $dir = '/some/dir';
    my $size;

    find( sub { -f and ( $size += -s _ ) }, $dir );

    print "Directory '$dir' contains $size bytes\n";



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn Guest

  6. #5

    Default Re: Portable unix du command


    On Tuesday, Nov 18, 2003, at 09:35 US/Pacific, Dan Muey wrote:
    [..]
    >
    > Thanks. What I really want to do is avoid system commands completely.
    > I could always traverse the directory adding the size of each file
    > together and do that all in Perl with no system commands but I figured
    > there'd already be a Module or something like this, so I want to do it
    > that way. Somthign like:
    >
    > my $sz = $module->size("/home/joemama");
    > # return sixze of file or of entire directory if specified file is a
    > directory
    I can appreciate the desire, but remember that
    "systems applications" - such as 'du' and 'ps'
    are there so that folks do not have to know all
    of the OS specific underlying 'system calls' that
    will flop into 'kernal space'.

    The fine folks who are maintaining the Proc::Table
    module deliver XS code that gets converted into
    the 'c' code that becomes the dynamically loadable
    library that is referenced in the "perl module"....

    Which is why I opted to take the 'sillier' path
    of simply working out which command line arguments
    were the 'right ones' for Me based upon the four
    or five basic *nix versions that I work with and
    then go through the process that way.

    If you want to have fun, then you will of course
    want to become friends with stat(), cf perldoc -f stat
    and/or read the POSIX module, cf perldoc POSIX.

    Or download a copy of the open source for 'du'
    and work out which parts of it you want to use
    as the basis for your own perl module.


    ciao
    drieux

    ---

    Drieux Guest

  7. #6

    Default RE: Portable unix du command

    Dan Muey wrote:
    > Howdy group.
    >
    > I have a need to get the size of a directory.
    > I could just execute unix's du command (my $sz = `du -sh /usr`;)
    > but was wondering if there's a better way to do it that is
    > more portable.
    > I looked on cpan but didn't see anythign that jumped out at me.
    There's a group working on implementing a number of Unix utilities in Perl.
    Here's their du page:

    [url]http://www.perl.com/language/ppt/src/du/index.html[/url]

    I don't know how portable it is...
    Bob Showalter Guest

  8. #7

    Default RE: Portable unix du command

    > Dan Muey wrote:
    > >
    > > Thanks. What I really want to do is avoid system commands
    > completely.
    > > I could always traverse the directory adding the size of each file
    > > together and do that all in Perl with no system commands
    > but I figured
    > > there'd already be a Module or something like this, so I
    > want to do it
    > > that way. Somthign like:
    >
    > You mean something like (untested):
    >
    > use File::Find;
    >
    > my $dir = '/some/dir';
    > my $size;
    >
    > find( sub { -f and ( $size += -s _ ) }, $dir );
    >
    > print "Directory '$dir' contains $size bytes\n";
    >
    >
    Awesome John! Works splendedly, this is exactly what I needed!
    >
    > John
    > --
    > use Perl;
    > program
    > fulfillment
    Dan Muey Guest

  9. #8

    Default RE: Portable unix du command

    Dan Muey wrote:
    >
    >> On Tuesday, Nov 18, 2003, at 09:20 US/Pacific, Dan Muey wrote:
    >>
    >> > Howdy group.
    >> >
    >> > I have a need to get the size of a directory.
    >> > I could just execute unix's du command (my $sz = `du -sh
    >> /usr`;) but
    >> > was wondering if there's a better way to do it that is more
    >> portable.
    >> > I looked on cpan but didn't see anythign that jumped out at me.
    >> >
    [snip]
    > Thanks. What I really want to do is avoid system commands completely.
    good decision.
    > I could always traverse the directory adding the size of each file
    > together and do that all in Perl with no system commands but I figured
    > there'd already be a Module or something like this, so I want to do it
    > that way. Somthign like:
    >
    > my $sz = $module->size("/home/joemama");
    try Filesys::DiskFree

    #!/usr/bin/perl -w
    use strict;

    use Filesys::DiskFree;

    my $h = Filesys::DiskFree->new;

    $h->df;
    $h->device('/usr');

    print "/usr has ",$h->avail('/usr')," bytes available\n";

    __END__

    prints:

    /usr has 111111111 bytes available

    david
    --
    s,.*,<<,e,y,\n,,d,y,.s,10,,s
    ..ss.s.s...s.s....ss.....s.ss
    s.sssss.sssss...s...s..s....
    ....s.ss..s.sss..ss.s....ss.s
    s.sssss.s.ssss..ss.s....ss.s
    ...s..sss.sssss.ss.sss..ssss.
    ...sss....s.s....ss.s....ss.s

    ,....{4},"|?{*=}_'y!'+0!$&;"
    ,ge,y,!#:$_(-*[./<-@{-},b-t,
    ..y...,$~=q~=?,;^_#+?{~,,$~=~
    y.!-&*-/:-@^_{}.a-t ().;s,;,
    );,g,s,s,$~s,g,y,y,%,,g,eval
    David Guest

  10. #9

    Default Re: Portable unix du command

    david wrote:
    > Dan Muey wrote:
    >
    > try Filesys::DiskFree
    >
    > #!/usr/bin/perl -w
    > use strict;
    >
    > use Filesys::DiskFree;
    >
    > my $h = Filesys::DiskFree->new;
    >
    > $h->df;
    > $h->device('/usr');
    >
    > print "/usr has ",$h->avail('/usr')," bytes available\n";
    >
    > __END__
    >
    > prints:
    >
    > /usr has 111111111 bytes available
    On a Unix system. unfortunately, the module is not that portable, depending
    on the Unix df command.

    Joseph

    R. Joseph Newton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139