The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
count the number of files which have a search string, but counting the file only once sudheshnaiyer UNIX for Dummies Questions & Answers 1 08-11-2007 10:50 AM
Number of files in a directory tantric UNIX for Dummies Questions & Answers 3 06-29-2007 02:22 PM
Counting files in one directory matrixtlm UNIX for Dummies Questions & Answers 10 06-15-2007 04:14 PM
Counting files in a directory that match a pattern dsravan Shell Programming and Scripting 31 07-24-2006 08:12 AM
rm files in a directory, looping, counting, then exit JporterFDX Shell Programming and Scripting 6 07-18-2002 05:56 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 09-05-2005
Registered User
 

Join Date: Sep 2005
Posts: 2
Counting number of files in a directory

Some simple questions from a simple man.

If i wanted to count the number of files contained within a directory, say /tmp would ls -l /tmp ¦ wc -l suffice and will it be accurate?

second one: How would i check the number of files with a certain string in the filename, in the same directory.

Appolgies if this has been answered before, i've been unable to find any posts.

Regards,

Al

Last edited by iamalex; 09-05-2005 at 06:53 AM.
Reply With Quote
Forum Sponsor
  #2  
Old 09-05-2005
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
ls -l /tmp | wc -l would count files, directories, and also the line that displays the total size of the listing.

The best way is
ls -1 /tmp | wc -l (ls -number_one) although this still shows directories too.

If you just want a count of files (including soft links, etc)....

echo "`ls -l | grep -v "^d" | wc -l` - 1" | bc

You can grep -v out stuff you don't want.

On the second question....

ls /my/dir/*some_string* | wc -l

Or to use regular expressions instead of globbing

ls /my/dir | grep "some_[Ss]tring" | wc -l

Many ways to skin these cats.....

Cheers
ZB
Reply With Quote
  #3  
Old 09-05-2005
Registered User
 

Join Date: Sep 2005
Posts: 2
Top Man

Great, thanks, i had a feeling i was going wrong somewhere, excellent reply and really helpful.

regards,

Al
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0