http://qs321.pair.com?node_id=73735

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

I would like to write a program to check all files sizes within the current directory. If a file is below 300K, it is deleted. I am current unsure about how exactly to check the file size. Any suggestions?

Replies are listed 'Best First'.
Re: checking file size
by tinman (Curate) on Apr 19, 2001 at 07:41 UTC

    stat, as noted above, is the internal Perl function.. There is also File::Stat which provides a slightly easier to use by-name interface to the file attribute functions.

Re: checking file size
by Albannach (Prior) on Apr 19, 2001 at 07:45 UTC
    For another option, you might try the -s file test operator.

    --
    I'd like to be able to assign to an luser

Re: checking file size
by Rhandom (Curate) on Apr 19, 2001 at 09:11 UTC
    You might try the following on for size:
    #!/usr/bin/perl use File::Find qw(find); kill_big_files( "/tmp", 300*1024 ); sub kill_big_files { my $dir = shift; my $size = shift; find( sub { # current file is in $_ # current dir is the directory $_ is in return if -d; # skip directories return if -l; # skip links return if $size > -s; print "removing $_\n"; unlink; }, $dir); }
    File::Find is a very useful tool. Very fast for making utilities which recurse directories.
Re: checking file size
by Masem (Monsignor) on Apr 19, 2001 at 07:31 UTC
    Try stat; specifically, you want the 8th element, the file size of said file.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain