LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2009, 03:23 AM   #1
Asteroid
LQ Newbie
 
Registered: May 2007
Posts: 27

Rep: Reputation: 15
PERL count files in a dir


Hi Guys,

I need to count files in a dir which were updated yesterday.
ls -lth | grep -i 'Jul 7' | wc -l

The dir holds files of last 15 days and total count is as 2067476.
Is it efficient to count the files using perl? I have developed the following perl script making use of system().

Can anybody coment, any other way without using system()
Code:
	

#!/usr/bin/perl 
  
 
@months = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec); 
($sec,$min,$hour,$monthday,$mon,$year,$wday,$yday,$isdst) = localtime(time); 
  
  
$month =$months[$mon]; 
 
############################################# 
# ls -lth | grep -i 'Jul 7' | wc -l  
############################################# 
 
$command = "ls -lth  | grep -i '" . $month . "  " . $monthday -1 . "' | wc -l"; 
  
$count = system($command); 
print "$count \n";
Thank you
Regards
@Asteroid
 
Old 07-08-2009, 04:22 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Use perl built in functions opendir to open the directory and then list files

Code:
#!/usr/bin/perl -w

$dirname = "/tmp";

opendir ( DIR, $dirname ) || die "Error in opening dir $dirname\n";
while( ($filename = readdir(DIR))){
     print("$filename\n");
}
closedir(DIR);

Last edited by PMP; 07-08-2009 at 04:23 AM.
 
Old 07-08-2009, 05:24 AM   #3
Asteroid
LQ Newbie
 
Registered: May 2007
Posts: 27

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by PMP View Post
Use perl built in functions opendir to open the directory and then list files

Code:
#!/usr/bin/perl -w

$dirname = "/tmp";

opendir ( DIR, $dirname ) || die "Error in opening dir $dirname\n";
while( ($filename = readdir(DIR))){
     print("$filename\n");
}
closedir(DIR);

But What about printing files from specific date. Listing file is not a problem, rather listing and counting files update on a specfic date is required here.
 
Old 07-08-2009, 05:34 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
use perl stat function to find the mofification time of a file, filter out the files using that

http://perldoc.perl.org/functions/stat.html
 
Old 07-08-2009, 10:22 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by PMP View Post
Use perl built in functions opendir to open the directory and then list files

Code:
#!/usr/bin/perl -w

$dirname = "/tmp";

opendir ( DIR, $dirname ) || die "Error in opening dir $dirname\n";
while( ($filename = readdir(DIR))){
     print("$filename\n");
}
closedir(DIR);

Wrong - since you'll also count directories.

Also wrong because a file or directory named "0" will yield FALSE and your while loop will stop iterating.

Also wrong because no strict is used.
 
Old 07-08-2009, 01:38 PM   #6
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

without perl:
Code:
find . -maxdepth 1 -type f -mtime -1 -print | wc -l
Jan
 
Old 07-09-2009, 12:51 AM   #7
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Thanks for the correction Sergei Steshenko.
Just to explain

Wrong - since you'll also count directories.
Just read the second post of mine which says

Quote:
use perl stat function to find the mofification time of a file, filter out the files using that
So when I am saying file it means files only not directories.

Also wrong because a file or directory named "0" will yield FALSE and your while loop will stop iterating.

This code is just to explain the opendir function, you can say it an idea of using the function.


Also wrong because no strict is used.

Well, this program compiles without complaining that strict is not being used, and up to my understanding using strict pragma is not compulsory. It is just a safe side of writing code.


Thank You
 
Old 07-09-2009, 02:09 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You'll need
Code:
  next if $file =~ /^\.\.?$/;     # skip . and ..
inside the loop to skip curr & parent dirs
 
Old 07-09-2009, 02:31 AM   #9
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
How about:

Code:
#!/usr/bin/perl

# Path to directory you want to count files in on your server.
$dir = "/path/some/where/";

$directory_count = 0;
$file_count=0;

opendir(DIR, $dir);
LINE: while($FILE = readdir(DIR)) {
next LINE if($FILE =~ /^\.\.?/);

## check to see if it is a directory
if(-d "$FILE"){
$directory_count++;
}
else {
$file_count++;
}
}
closedir(DIR);

print "Directories: $directory_count\n";
print "Files: $file_count\n";

exit;
 
Old 07-09-2009, 06:30 AM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by PMP View Post
...
Well, this program compiles without complaining that strict is not being used, and up to my understanding using strict pragma is not compulsory. It is just a safe side of writing code.


Thank You
Of course - as long as you believe you do not make mistakes.

In production I ruthlessly eradicate Perl scripts without 'use strict;' - years of experiences have shown that real life scripts without 'use strict;' do have bugs which 'use strict;' shows.
 
Old 07-09-2009, 06:34 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by micxz View Post
How about:

Code:
#!/usr/bin/perl

# Path to directory you want to count files in on your server.
$dir = "/path/some/where/";

$directory_count = 0;
$file_count=0;

opendir(DIR, $dir);
LINE: while($FILE = readdir(DIR)) {
next LINE if($FILE =~ /^\.\.?/);

## check to see if it is a directory
if(-d "$FILE"){
$directory_count++;
}
else {
$file_count++;
}
}
closedir(DIR);

print "Directories: $directory_count\n";
print "Files: $file_count\n";

exit;
Without going into details - why do you have quotes in

Code:
"$FILE"
?

And if you wrote

Code:
if(-d $FILE)
it would still be a mistake (not because of absence/presence of quotes).

And since '.' and '..' are directories, checking for being a directory would suffice.

And yet another Perl script without warnings and strict enabled, with all the variables being global ...
 
Old 07-09-2009, 06:37 AM   #12
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Well that what I said, Its a practice that one needs to follow but not necessary. And saying that wrong is wrong in itself
 
Old 07-09-2009, 12:59 PM   #13
micxz
Senior Member
 
Registered: Sep 2002
Location: CA
Distribution: openSuSE, Cent OS, Slackware
Posts: 1,131

Rep: Reputation: 75
Quote:
Originally Posted by Sergei Steshenko View Post
Without going into details - why do you have quotes in

Code:
"$FILE"
I'm not sure, trial and error. It's works so I didn't think it was wrong.

Quote:
Originally Posted by Sergei Steshenko View Post
And if you wrote

Code:
if(-d $FILE)
it would still be a mistake (not because of absence/presence of quotes).

And since '.' and '..' are directories, checking for being a directory would suffice.

And yet another Perl script without warnings and strict enabled, with all the variables being global ...
I didn't think checking for . or .. was necessary as in most cases. Hence the:
=~ /^\.\.?/);

Is this acceptable in your book:
Code:
#!/usr/bin/perl

use strict;

my $dir = "./";

my $directory_count = 0;
my $file_count=0;

opendir(DIR, $dir);
LINE: while(my $FILE = readdir(DIR)) {
next LINE if($FILE =~ /^\.\.?/);

if(-d $FILE){
$directory_count++;
}
else {
$file_count++;
}
}
closedir(DIR);

print "Directories: $directory_count\n";
print "Files: $file_count\n";

exit;

Last edited by micxz; 07-09-2009 at 01:02 PM. Reason: spell
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command to display /dir, /dir/sub, /dir/sub/files knockout_artist Linux - Newbie 9 10-25-2007 02:57 PM
Count # of dir or # of files melissatoday Linux - Newbie 1 06-03-2006 11:03 AM
Recursively count lines of code in source dir heirarchy michaelsanford Programming 8 04-01-2006 04:59 PM
Q. moving /bin dir files to /dev dir?? Texas_student Linux - Software 2 03-26-2006 11:42 PM
CGI perl links to files in Dir ocularbob Programming 3 04-29-2003 08:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration