Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

PERL count files in a dir

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-08-2009
Asteroid Asteroid is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 14 August 2013, 9:54 AM EDT
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 07-08-2009
blitzer blitzer is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 22 December 2009, 12:45 PM EST
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
I would avoid at any price to call system from a perl script.
Here's a quick try

Code:
perl -e 'opendir (DIR, $ARGV[0]) ; @count = readdir(DIR); $count = @count; print $count-2,"\n"' /path/to/your/directory



---------- Post updated at 11:27 ---------- Previous update was at 11:25 ----------

Edit: added the -2 in order not to count "." and ".."
Sponsored Links
    #3  
Old Unix and Linux 07-08-2009
Asteroid Asteroid is offline
Registered User
 
Join Date: Apr 2007
Last Activity: 14 August 2013, 9:54 AM EDT
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Thx blitzer for the reply, what if I want to count files updated ona specfic date.
I mean, I don't want to count all the files in the directory. I want to count files updated/created on a specfic date (say today 08-07-2009).

An equivalent in bash is some thing like this:


Code:
ls -lth | grep 'Jul  8' | wc -l

This is how I can count files, updated on 8th july 2009 (using bash)


Thank you.
    #4  
Old Unix and Linux 07-08-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
 
Join Date: Jan 2008
Last Activity: 11 September 2009, 3:44 PM EDT
Posts: 729
Thanks: 0
Thanked 0 Times in 0 Posts
Your question has been answered on another forum
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Modify a perl script to find and count richsark Shell Programming and Scripting 4 04-01-2009 02:02 PM
Perl - Count occurences TimHortons Shell Programming and Scripting 22 01-31-2009 12:30 AM
Perl/shell script count the lines pistachio UNIX for Dummies Questions & Answers 3 09-24-2008 04:26 PM
grep all records in a file and get a word count -perl meghana Shell Programming and Scripting 4 02-13-2008 09:06 PM
hw can i count the number of character in a file by perl trupti_rinku Shell Programming and Scripting 1 10-31-2006 06:27 AM



All times are GMT -4. The time now is 09:47 PM.