The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 01:50 PM
Number of files in a directory tantric UNIX for Dummies Questions & Answers 3 06-29-2007 05:22 PM
Counting files in one directory matrixtlm UNIX for Dummies Questions & Answers 10 06-15-2007 07:14 PM
Counting files in a directory that match a pattern dsravan Shell Programming and Scripting 31 07-24-2006 11:12 AM
rm files in a directory, looping, counting, then exit JporterFDX Shell Programming and Scripting 6 07-18-2002 08:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-05-2005
iamalex iamalex is offline
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 09:53 AM..
  #2 (permalink)  
Old 09-05-2005
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
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
  #3 (permalink)  
Old 09-05-2005
iamalex iamalex is offline
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
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:03 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0