ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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()
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.
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.
#!/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;
...
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.
#!/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 ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.