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.

Count the number of files in a directory

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 08-16-2007
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 31 August 2015, 9:59 AM EDT
Location: Foxborough, MA
Posts: 7,872
Thanks: 176
Thanked 667 Times in 624 Posts

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

Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 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 Unix and Linux 08-16-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 28 August 2015, 12:59 AM EDT
Location: classification algos
Posts: 3,206
Thanks: 19
Thanked 30 Times in 27 Posts

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

Sponsored Links
    #6  
Old Unix and Linux 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 Unix and Linux 08-16-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 28 August 2015, 12:59 AM EDT
Location: classification algos
Posts: 3,206
Thanks: 19
Thanked 30 Times in 27 Posts

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

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:18 AM.