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
Help with counting files please gerard1 Shell Programming and Scripting 6 09-25-2006 10:37 AM
Counting files in a directory that match a pattern dsravan Shell Programming and Scripting 31 07-24-2006 08:12 AM
Line counting in Directory MobileUser UNIX for Dummies Questions & Answers 3 02-25-2006 01:15 AM
Counting number of files in a directory iamalex UNIX for Dummies Questions & Answers 2 09-05-2005 07:13 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 06-12-2007
Registered User
 

Join Date: Jun 2007
Posts: 3
Counting files in one directory

I did a search on this forum and found this command to count the total of files in one directory:

ls -l | wc -l

I did a test by creating one file in a folder and issue the command. But it return value "2".

Abit confused now. Since there is one file in the directory, isn't it it should return value "1" ?

At the same time I tried "ls -lrt | more", it return "total xxxxx". Is this figure showing the number of lines?

I want to do a count on a directory to get the total files. Please advise. Appreciate. Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 06-12-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
The reason you are seeing 2 is because of the output of ls -l. Rather you may want to use ls -1 Notice the digit 1 against the letter l

Code:
[/tmp/test]$ ls -l
total 0
-rw-r--r--  1 xxxxxxxx g900 0 Jun 12 22:05 test
[/tmp/test]$ ls -l | wc -l
2
[/tmp/test]$ ls -1
test
[/tmp/test]$ ls -1 | wc -l
1
[/tmp/test]$
Reply With Quote
  #3  
Old 06-13-2007
Registered User
 

Join Date: Jun 2007
Posts: 3
Hi... thanks for the reply.

When I do a "ls -l", i get a return of "total 4000". Even I minus 1, still i will get a value of 3999.

$ls -l | more
total 4000

But when I did a ls -1|wc -l. I get a different figure of "310".

ls -1 | wc -l
310

Is there a difference on the two values? I thought both the command should return the same figure. Please correct me if i'm wrong. Need some explanation. Thanks.
Reply With Quote
  #4  
Old 06-13-2007
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
Post the output of ls -l
Reply With Quote
  #5  
Old 06-13-2007
Registered User
 

Join Date: Jun 2007
Posts: 3
Hi,

This is the actual figure in my machine

$ls -l | more
total 4696146

$ls -1| wc -l
4312

Thanks.
Reply With Quote
  #6  
Old 06-13-2007
Registered User
 

Join Date: Jan 2007
Posts: 13
The "total" is something to do with the amount of disk space that directory is using I think. Its nothing to do with the number of files in the directory.

wc -l seems the way to go.
Reply With Quote
  #7  
Old 06-13-2007
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
total in ls command

Pls check this thread,
http://unix.com/showthread.php?t=367...light=ls+total

Thanks
Nagarajan G
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:53 PM.


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