CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
how to find all files on the directory by size and time

 



digioleg54
User

Mar 30, 2017, 11:51 AM

Post #1 of 13 (5245 views)
how to find all files on the directory by size and time Can't Post

I need to find files by size and older than 5 days, gzip them or remove. Cannot find anything. I know File::Find module, but there are no options for size and / or time. Could you please help me4?


FishMonger
Veteran / Moderator

Mar 30, 2017, 12:00 PM

Post #2 of 13 (5244 views)
Re: [digioleg54] how to find all files on the directory by size and time [In reply to] Can't Post

Use File::Find::Rule http://search.cpan.org/~rclamp/File-Find-Rule-0.34/lib/File/Find/Rule.pm


digioleg54
User

Mar 30, 2017, 12:23 PM

Post #3 of 13 (5241 views)
Re: [FishMonger] how to find all files on the directory by size and time [In reply to] Can't Post

Nothing new.
I tried


opendir DIRHDL, "." || die "Cannot open dir $!";
my @filelist = readdir (DIRHDL);
closedir (DIRHDL);
foreach my $filename ( @filelist )
{
my $filesize = find ( size => '>10K');
print "SIZE $filesize\n"
}


Can't stat >10B: No such file or directory
at ./oleg.pl line 63
SIZE


digioleg54
User

Mar 30, 2017, 12:39 PM

Post #4 of 13 (5234 views)
Re: [FishMonger] how to find all files on the directory by size and time [In reply to] Can't Post

Below is working, but I have to get them together and zip

opendir DIRHDL, "." || die "Cannot open dir $!";
my @filelist = readdir (DIRHDL);
closedir (DIRHDL);
foreach my $filename ( @filelist )
{
$mtime = (stat("$filename"))[9] || die "cannot stat file $!";
$size = (stat("$filename"))[7]; # size in bytes
print "$mtime\n";
print "$size\n";
}


digioleg54
User

Mar 30, 2017, 12:55 PM

Post #5 of 13 (5230 views)
Re: [FishMonger] how to find all files on the directory by size and time [In reply to] Can't Post

Good news. We don't have File::Find::Rule

I will use in the loop

if ( -M $filename > 10) {unlink $filename}

The question is how to find all files by size and time and gzip them???


digioleg54
User

Mar 30, 2017, 2:05 PM

Post #6 of 13 (5220 views)
Re: [FishMonger] how to find all files on the directory by size and time [In reply to] Can't Post

This is better solution:

die unless opendir DIR, ".";
foreach my $file (grep {-f && (150 <= -M)} readdir DIR)
{
unlink $file;
}
closedir (DIR);

But the problem is that on the other directory I have to find all files by size and mtime and zip them
Do you know how to do it?


FishMonger
Veteran / Moderator

Mar 30, 2017, 2:21 PM

Post #7 of 13 (5213 views)
Re: [digioleg54] how to find all files on the directory by size and time [In reply to] Can't Post

I'd use Archive::Tar or similar module.

If you don't want to use modules, then you should probably write a shell script instead of a perl script.


(This post was edited by FishMonger on Mar 30, 2017, 2:21 PM)


digioleg54
User

Mar 30, 2017, 4:39 PM

Post #8 of 13 (5203 views)
Re: [FishMonger] how to find all files on the directory by size and time [In reply to] Can't Post

If we have it. Zip we definitely don't have. And this is the case that in a new server we don't have shell. Perl only


digioleg54
User

Mar 30, 2017, 4:56 PM

Post #9 of 13 (5201 views)
Re: [FishMonger] how to find all files on the directory by size and time [In reply to] Can't Post

Could you please tell me the logic:
How to read files on the directory and zip them all. I will use `zip .....` but I don't know how to get files together to one file and then zip it


Laurent_R
Veteran / Moderator

Mar 30, 2017, 11:22 PM

Post #10 of 13 (5194 views)
Re: [digioleg54] how to find all files on the directory by size and time [In reply to] Can't Post


In Reply To
And this is the case that in a new server we don't have shell. Perl only

How can that be? How do you invoke Perlor a Perl program if it's not from the shell?


digioleg54
User

Mar 31, 2017, 5:15 AM

Post #11 of 13 (5191 views)
Re: [Laurent_R] how to find all files on the directory by size and time [In reply to] Can't Post

It will work on web server, remotely connecting to UNIX


Laurent_R
Veteran / Moderator

Mar 31, 2017, 9:48 AM

Post #12 of 13 (5188 views)
Re: [digioleg54] how to find all files on the directory by size and time [In reply to] Can't Post

I have yet to see a Unix server without a shell. I don't think that exists (or, at least, I've never seen any).

Maybe there is a misunderstanding and you don't fully understand what we mean by shell?


digioleg54
User

Mar 31, 2017, 10:46 AM

Post #13 of 13 (5187 views)
Re: [Laurent_R] how to find all files on the directory by size and time [In reply to] Can't Post

Laurent,
My script will run on web apache. I can copy modules, which I needed. There is no shell, no shell command will be used, just Perl modules. That is why I ask you "How to copy some Perl modules to apache

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives