Re: removing files in unix or linux using glob?
On Fri, Nov 21, 2008 at 16:38, Richard Lee <[EMAIL PROTECTED]> wrote: > Richard Lee wrote: >> >> Richard Lee wrote: >>> >>> stion on this.. suppose there is 3 files in temp directory >>> >>> /tmp/yahoo1 >>> /tmp/yahoo2 >>> /tmp/yahoo3 >>> >>> and I wanted to take the last file that was created.. would this work? >>> >>> my $filename = shift; >>> >>> my @file_1 = ; >>> my $file_1 = $file_1[-1]; >>> push @files, $file_1; >> >> i am not 100% sure if this logic is correct or not. >> >> my %big_hash; >> for (@file_1) { >>$big_hash{$_} = (stat($_))[9]; >> } >> >> my @keys = sort { $big_hash{$a} <=> $big_hash{b} } keys %big_hash; >> my $file_1 = $keys[-1]; >> push @files, $file_1; >> > I am missing a $ in $big_hash{b}.. and i see that this method works.. but is > there easier and better(correct) way to do this ??? If the number of files is fewer than a two hundred this is fine, but if it is more then you really want to use a min routine rather than sort to find the oldest file: #FIXME: if two files are the oldest, we can't predict which one will be deleted my $oldest_name; my $oldest_date = 0; for my $file () { if ($oldest_date >= (stat $file)[8]) { # _ is used here to get the cached info # from the last stat call $oldest_date = (stat _)[8]; last stat call $oldest_name = $file; } } die "no files found" unless defined $oldest_name; print "the oldest file is $oldest_name\n"; -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: removing files in unix or linux using glob?
Richard Lee wrote: Richard Lee wrote: stion on this.. suppose there is 3 files in temp directory /tmp/yahoo1 /tmp/yahoo2 /tmp/yahoo3 and I wanted to take the last file that was created.. would this work? my $filename = shift; my @file_1 = ; my $file_1 = $file_1[-1]; push @files, $file_1; i am not 100% sure if this logic is correct or not. my %big_hash; for (@file_1) { $big_hash{$_} = (stat($_))[9]; } my @keys = sort { $big_hash{$a} <=> $big_hash{b} } keys %big_hash; my $file_1 = $keys[-1]; push @files, $file_1; I am missing a $ in $big_hash{b}.. and i see that this method works.. but is there easier and better(correct) way to do this ??? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: removing files in unix or linux using glob?
Richard Lee wrote: stion on this.. suppose there is 3 files in temp directory /tmp/yahoo1 /tmp/yahoo2 /tmp/yahoo3 and I wanted to take the last file that was created.. would this work? my $filename = shift; my @file_1 = ; my $file_1 = $file_1[-1]; push @files, $file_1; i am not 100% sure if this logic is correct or not. my %big_hash; for (@file_1) { $big_hash{$_} = (stat($_))[9]; } my @keys = sort { $big_hash{$a} <=> $big_hash{b} } keys %big_hash; my $file_1 = $keys[-1]; push @files, $file_1; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: removing files in unix or linux using glob?
Mr. Shawn H. Corey wrote: See: * perldoc -f unlink * perldoc -f glob * perldoc perlfunc and search for -f under "Alphabetical Listing of Perl Functions" one more question on this.. suppose there is 3 files in temp directory /tmp/yahoo1 /tmp/yahoo2 /tmp/yahoo3 and I wanted to take the last file that was created.. would this work? my $filename = shift; my @file_1 = ; my $file_1 = $file_1[-1]; push @files, $file_1; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: removing files in unix or linux using glob?
Mr. Shawn H. Corey wrote: for my $file ( glob( '/tmp/yahoo.*' ) ){ unlink $file if -f $file; } See: * perldoc -f unlink * perldoc -f glob * perldoc perlfunc and search for -f under "Alphabetical Listing of Perl Functions" thanks, I tried if ( -f q#/tmp/# ) { system("rm -rf /tmp/"); } and worked.. but above seems better.. i will read up on those as well.. thank you. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: removing files in unix or linux using glob?
On Wed, 2008-11-19 at 12:12 -0500, Richard Lee wrote: > I thought I could do this, > > if ( -f q#/tmp/yahoo.* ) { > system("rm -rf /tmp/yahoo.*"); > } > > what am i missing? > for my $file ( glob( '/tmp/yahoo.*' ) ){ unlink $file if -f $file; } See: * perldoc -f unlink * perldoc -f glob * perldoc perlfunc and search for -f under "Alphabetical Listing of Perl Functions" -- Just my 0.0002 million dollars worth, Shawn The map is not the territory, the dossier is not the person, the model is not reality, and the universe is indifferent to your beliefs. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: removing files in unix or linux using glob?
On Wed, Nov 19, 2008 at 12:12:50PM -0500, Richard Lee wrote: > I thought I could do this, > > if ( -f q#/tmp/yahoo.* ) { >system("rm -rf /tmp/yahoo.*"); > } > > what am i missing? A hash, the glob() function, or perhaps just that the test is superfluous. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
removing files in unix or linux using glob?
I thought I could do this, if ( -f q#/tmp/yahoo.* ) { system("rm -rf /tmp/yahoo.*"); } what am i missing? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/