Count the number of files in a directory | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Count the number of files in a directory

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-16-2007
Raynon Raynon is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 18 April 2011, 3:56 AM EDT
Location: Sg
Posts: 353
Thanks: 0
Thanked 2 Times in 2 Posts
Count the number of files in a directory

Hi All,

How do i find out the number of files in a directory using unix command ?
Sponsored Links
    #2  
Old 08-16-2007
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 24 October 2014, 7:17 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts

Code:
ls -l . | egrep -c '^-'

Sponsored Links
    #3  
Old 08-16-2007
Raynon Raynon is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 18 April 2011, 3:56 AM EDT
Location: Sg
Posts: 353
Thanks: 0
Thanked 2 Times in 2 Posts
Hi vgersh99,

Thanks it works!
If i want to find the number of *sorts file in a directory, what would be the modification needed to the code?
    #4  
Old 08-16-2007
lorcan lorcan is offline
Registered User
 
Join Date: May 2007
Last Activity: 13 August 2012, 12:14 AM EDT
Posts: 224
Thanks: 0
Thanked 2 Times in 2 Posts

Code:
ls -l * | grep '^-' | grep sorts | wc -l

Sponsored Links
    #5  
Old 08-16-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts

Code:
ls -l *sorts | awk '/^-/{c++}END{ print c }'

Sponsored Links
    #6  
Old 08-16-2007
Raynon Raynon is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 18 April 2011, 3:56 AM EDT
Location: Sg
Posts: 353
Thanks: 0
Thanked 2 Times in 2 Posts
Hi Lorcan,

Thanks but if there's no *sorts file in the directory, the output will be reflected below. Is there any way i can remove the "No match" output, leaving only the "0" as the output ?

$ ls -l *sorts | wc -l
No match
0
Sponsored Links
    #7  
Old 08-16-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts

Code:
ls -l *sorts 2>/dev/null | awk '/^-/{c++}END{ print c }'

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to count the number of files starting with a pattern in a Directory atechcorp Shell Programming and Scripting 7 07-24-2011 03:36 PM
Count number of files in directory excluding existing files ammu UNIX for Dummies Questions & Answers 3 08-25-2010 01:54 PM
perl script on how to count the total number of lines of all the files under a directory adityam Shell Programming and Scripting 5 07-07-2010 04:36 AM
Read directory files and count number of lines jl487 UNIX for Dummies Questions & Answers 4 04-11-2010 07:16 PM
count number of files in a directory finalight Shell Programming and Scripting 5 05-14-2009 07:57 AM



All times are GMT -4. The time now is 09:23 AM.