Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

size of directory

by darshan_atha (Acolyte)
on Oct 10, 2002 at 06:43 UTC ( [id://204104]=perlquestion: print w/replies, xml ) Need Help??

darshan_atha has asked for the wisdom of the Perl Monks concerning the following question:

hi can anybody help me in creating a directory in perl with 777 mode i used mkdir(file ,mode); it is creating the directory but not with proper mode also wated to know the size of that directory i am using apache server thanx.

Replies are listed 'Best First'.
Re: size of directory
by Zaxo (Archbishop) on Oct 10, 2002 at 06:48 UTC

    The mkdir builtin creates the directory with mode 0777 if the mask argument is omitted. There is no mode argument.

    mkdir /path/to/newdir;

    What do you mean by 'size' in the title? An empty directory has a size of one disk block.

    Update: ++podmaster reminds me of umask, which sets the current default mask applied to the creation of all new files (including directories), returning the old default mask to ease its restoration.

    Update 2: =sjs=, your perl -e'mkdir{"t")' works for me. Are you running some blighted shell that demands double quotes for -e arguments? If so, try <code>perl -e"mkdir(q(t))"

    After Compline,
    Zaxo

      when I

      > perl -e 'mkdir("t")' Not enough arguments for mkdir at -e line 1, at end of line Execution of -e aborted due to compilation errors.

      What do you mean??

Re: size of directory
by mikeirw (Pilgrim) on Oct 10, 2002 at 07:08 UTC

    If you're wanting to sum the sizes of all files within a directory, this should do it:

    my $dir = '/path/to/dir'; my $dir_size = 0; my $file; opendir DIR, $dir or die "Unable to open $dir: $!\n"; $dir_size += -s "$dir/$file" while defined( $file = readdir DIR ); closedir DIR; print $dir_size;

    This does not descend into subdirectories, though.

Re: size of directory
by zentara (Archbishop) on Oct 10, 2002 at 15:23 UTC
    Here's a dirsize method using File::Find.
    #!/usr/bin/perl -w use File::Find; use strict; my $total = 0; my $dir = $ARGV[0] || '.'; find sub {$total += -s $_;},$dir; my $megs = sprintf "%5.2f",($total/(1024*1024)); print "Total size of files in $dir:$megs Mbytes\n";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-19 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found