I will present a modified version of your code, then some explanation.
#!/usr/bin/perl
use strict;
use warnings;
my $dir = ".";
my $directory_count = 0;
my $file_count=0;
my $changed_yesterday_count=0;
opendir(DIR, $dir) or die "unable to open $dir";
while(my $FILE = readdir(DIR))
{
next if($FILE =~ /^\.$|^\.\.$/); # .file_name is "hidden" file
# . and .. are standard director
+ies
if(-d "$dir/$FILE") #need to use a full path
{
$directory_count++;
}
elsif (-f _) #simple file (not link,pipe,etc)
{
$file_count++;
}
if (-C _ == 1) # inode changed one day ago
{ # see notes below, this is
# not perfect
$changed_yesterday_count++;
}
}
closedir(DIR);
print "Directories: $directory_count\n";
print "Files: $file_count\n";
print "Dirs/files changed 1 day ago: $changed_yesterday_count\n";
__END__
example print:
Directories: 5
Files: 368
Dirs/files changed 1 day ago: 0
- Others have pointed out a potential flaw in you regex to skip the . and .. directories
- readdir() only returns the file names, not full path. You need to create the full path for
use with a file test or other operation.
- A test like -d "$dir/$FILE" actually fetches the whole stat data structure which is
relatively "expensive". To do another test on that exact same file, the special variable "_"
can be used. That just looks at the cached result of the previous file test operation. This just makes
further file tests run faster because the file system doesn't need to be accessed again.
- There are more than just files and directories, there are other things like pipes and
symbolic links. So, just because something is not a directory, that does mean that it is for
sure a simple file. In most cases that will be true, but not always.
- Unix does not have a file "creation time". I would call that the "born on" date. This is
available on a Windows file system, but requires use of a special api. In almost all cases
on Unix, you are interested in the inode change time. This time changes if the file is modified or
if the permissions (user/group/others) is modified.
- For your application, looks to me like the -C file test will do the trick. This yields
how long ago the ctime changed measured in days.
- Some links for further study:
file stat function
file test operators
Please explain -C inode
can google "st_ctime"