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

Get disk usage without modules

by borodache (Novice)
on Feb 05, 2018 at 13:16 UTC ( #1208480=perlquestion: print w/replies, xml ) Need Help??
borodache has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am looking for a code in perl that can tell me the disk usage of directories, but without using any outside module. No "df", no "use Filesys::DiskUsage qw/du/;" and etc. it should work on perl v5.8.4 without any additional libraries. Any help will be highly appreciated. Thanks in Advance

Replies are listed 'Best First'.
Re: Get disk usage without modules
by Eily (Prior) on Feb 05, 2018 at 16:38 UTC

    without using any outside module
    Why? I doubt this is because you can't run someone else's code because that's what you are asking for. And since Filesys::DiskUsage is pure perl, it just happens to be perl code that does exactly what you want, you can access the source (less than 200 lines of code, if you don't count File::Basename), and get your inspiration from there, or even copy the whole thing and remove what you don't need. The fact that it is a module just means that you can also install it through CPAN, and there's a big chance that it has been better tested than code someone would give as an answer to your request.

Re: Get disk usage without modules
by Laurent_R (Canon) on Feb 05, 2018 at 15:13 UTC
    Hi borodache,

    as a starting point, you might want to take a look at the following very basic subroutine which recursively traverses the subdirectory tree:

    sub traverse_dir { my $dir = shift; my @entries = glob "$dir/*"; for my $entry (@entries) { if (-f $entry) { print "$entry:", -s $entry, "\n"; } elsif (-d $entry) { traverse_dir($entry); } else # perhaps do something for entries which are neither files + nor subdirectories, if that sort of thing exists } } } traverse_dir ".";
    Many things would need to be added: management of symbolic links in accordance with your needs, adding accumulators for cumulative size of directories, etc.

    Also note that I have used the -d, -f and -s file test operators for simplicity, but in a real program, I would probably use the stat (and possibly lstat) built-in function instead. Also, you might want to replace the glob function call by a opendir and readdir combination, together with appropriate calls to grep to get rid of entries such as . and .. (which are skipped by glob).

    Update: see To glob or not to glob for a discussion of glob and some of its advantages and disadvantages.

Re: Get disk usage without modules
by davido (Cardinal) on Feb 05, 2018 at 16:27 UTC

    Are core modules permissible?

    File::Find can be used to traverse a portion of a filesystem and tally things up. It's a rather inefficient solution if you want an entire volume's usage. The utilitydf would be better for information on an entire volume, and can be wrapped in a Perl call. And if you need to tally usage of a portion of a volume du would be useful. But if du and df are off the table, File::Find along with stat are good starting points.

    The File::Find module has been bundled with Perl since Perl 5 began. If it is not allowed as part of the solution, then push back on that requirement, because the same logic would also prevent using strict and warnings.


    Dave

Re: Get disk usage without modules
by karlgoethebier (Monsignor) on Feb 05, 2018 at 18:16 UTC
    "...without..."

    Still worth to read, especially what merlyn wrote 16 years ago.

    You may cheat (AKA code reuse) from File::Find.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Get disk usage without modules
by Laurent_R (Canon) on Feb 05, 2018 at 20:49 UTC
    Cross-posted on the Perl Guru forum: http://perlguru.com/gforum.cgi?post=84649;#84649.

    There is nothing wrong with cross-posting questions, but it is considered proper to inform your reader to avoid duplication of work on various parts of the Internet.

Re: Get disk usage without modules
by borodache (Novice) on Feb 06, 2018 at 15:59 UTC
    Thank you for the reply everyone. Each of you put some thoughts inside and gave a different idea that helped me to handle this problem. Thanks a lot! The final solution I made was using @karlgoethebier post (talking about the 16 years old post of @merlyn), I combined it with stat function, which @Laurent_R and @davido suggested. Thanks a lot!
Re: Get disk usage without modules
by Anonymous Monk on Feb 05, 2018 at 13:44 UTC
    Heh ... Surely since time immemorimorkn some foody alroodhy wrootyit

    ppt du

    disk usage

Re: Get disk usage without modules
by Anonymous Monk on Feb 06, 2018 at 14:50 UTC
    There are very good reasons why you should use an existing, battle-tested module instead of trying to "roll your own." The CPAN library contains several tested ways to do this, and if you simply leverage one of them (without bothering to peek under the hood), both you and everyone who will follow in your footsteps will know that it works.
      There are very good reasons why you should use an existing, battle-tested module instead of trying to "roll your own." The CPAN library contains several tested ways to do this, and if you simply leverage one of them (without bothering to peek under the hood), both you and everyone who will follow in your footsteps will know that it works.

      And yet you list none of those reasons; and of course, everything on cpan always works, all the time. Yes Sundail; I recognise you even incognito.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1208480]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2018-10-16 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (86 votes). Check out past polls.

    Notices?