Re: Determining the number of files in a directory

2007-11-03 Thread Pietro Cerutti
White Hat wrote:
> This is probably a dumb question; however, I never let a little thing like 
> that bother me in the past.
>  
> Using FreeBSD-6.2 and Bash, how do I determine the number of files in a given 
> directory? I have tried all sorts of combinations using different flags with 
> the 'ls' command; however, none of them displays the number of files in the 
> directory.
>  
> Other than, by writing a script to accomplish this feat, how could I achieve 
> my goal?

I find that the most intuitive way is to use something like:

find . -maxdepth 1 | wc -l

Where you can specify how many levels of subdirectories to include in
the count. Please note that count also includes the current directory (".").

To exclude hidden files (.*), use

find * -maxdepth 0 | wc -l

or

expr `ls -l | wc -l` - 1

instead.



>  
> Thanks!
>  


-- 
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp



signature.asc
Description: OpenPGP digital signature


Re: Determining the number of files in a directory

2007-11-03 Thread Gary Kline
On Sat, Nov 03, 2007 at 09:07:18PM +0100, cpghost wrote:
> On Sat, 3 Nov 2007 10:44:43 -0400
> John Nielsen <[EMAIL PROTECTED]> wrote:
> 
> > On Saturday 03 November 2007, Daniel Bye wrote:
> > > On Sat, Nov 03, 2007 at 12:41:51PM +, Daniel Bye wrote:
> > > > On Sat, Nov 03, 2007 at 05:27:06AM -0700, White Hat wrote:
> > > > > This is probably a dumb question; however, I never let a little
> > > > > thing like that bother me in the past.
> > > >
> > > > Heheh! You and many more, my friend, myself absolutely included!
> > > >
> > > > > Using FreeBSD-6.2 and Bash, how do I determine the number of
> > > > > files in a given directory? I have tried all sorts of
> > > > > combinations using different flags with the 'ls' command;
> > > > > however, none of them displays the number of files in the
> > > > > directory.
> > > >
> > > >  $ ls | wc -l
> > > >
> > > > will show you how many files and directories in the current
> > > > (target) directory. To count just files, and exclude directories,
> > > > you could try something like
> > > >
> > > >  $ find /target/directory -type f -print | wc -l
> > >
> > > Except of course, that would descend into the subdirectories you're
> > > trying not to count... Sorry - an object lesson in not hitting send
> > > before you've tested what you scribbled.
> > 
> > find /target/directory -type f -maxdepth 1 | wc -l
> > 
> > should do the trick. See also man find and man wc, of course.
> 
> That's better than ls(1), which is terribly slow at displaying
> (actually: at sorting) large directories. In this case, better
> turn off sorting with 'ls -f':
> 
> $ time ls -f /usr/local/news/News | wc -l
> 0.42 real 0.29 user 0.07 sys
>35935
> 
> $ time ls /usr/local/news/News | wc -l
>   147.02 real33.92 user 0.07 sys
>35935


And ls -lf makes working with awk faster, too. E.g:

 % ls -lf | awk '$8 == 2007 {print $9}'
 % ls -ltf | awk '$8 == 2007 {print $9}'

If you've got  a large number of files, ls -lf is roughly 
twice as fast.


gary

> 
> -cpghost.
> 
> -- 
> Cordula's Web. http://www.cordula.ws/
> ___
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "[EMAIL PROTECTED]"

-- 
  Gary Kline  [EMAIL PROTECTED]   www.thought.org  Public Service Unix
  http://jottings.thought.org   http://transfinite.thought.org

___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Determining the number of files in a directory

2007-11-03 Thread cpghost
On Sat, 3 Nov 2007 10:44:43 -0400
John Nielsen <[EMAIL PROTECTED]> wrote:

> On Saturday 03 November 2007, Daniel Bye wrote:
> > On Sat, Nov 03, 2007 at 12:41:51PM +, Daniel Bye wrote:
> > > On Sat, Nov 03, 2007 at 05:27:06AM -0700, White Hat wrote:
> > > > This is probably a dumb question; however, I never let a little
> > > > thing like that bother me in the past.
> > >
> > > Heheh! You and many more, my friend, myself absolutely included!
> > >
> > > > Using FreeBSD-6.2 and Bash, how do I determine the number of
> > > > files in a given directory? I have tried all sorts of
> > > > combinations using different flags with the 'ls' command;
> > > > however, none of them displays the number of files in the
> > > > directory.
> > >
> > >  $ ls | wc -l
> > >
> > > will show you how many files and directories in the current
> > > (target) directory. To count just files, and exclude directories,
> > > you could try something like
> > >
> > >  $ find /target/directory -type f -print | wc -l
> >
> > Except of course, that would descend into the subdirectories you're
> > trying not to count... Sorry - an object lesson in not hitting send
> > before you've tested what you scribbled.
> 
> find /target/directory -type f -maxdepth 1 | wc -l
> 
> should do the trick. See also man find and man wc, of course.

That's better than ls(1), which is terribly slow at displaying
(actually: at sorting) large directories. In this case, better
turn off sorting with 'ls -f':

$ time ls -f /usr/local/news/News | wc -l
0.42 real 0.29 user 0.07 sys
   35935

$ time ls /usr/local/news/News | wc -l
  147.02 real33.92 user 0.07 sys
   35935

-cpghost.

-- 
Cordula's Web. http://www.cordula.ws/
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Determining the number of files in a directory

2007-11-03 Thread Erik Cederstrand

Daniel Bye wrote:

On Sat, Nov 03, 2007 at 12:41:51PM +, Daniel Bye wrote:

[...]
Using FreeBSD-6.2 and Bash, how do I determine the number of files in a 
given directory? I have tried all sorts of combinations using different 
flags with the 'ls' command; however, none of them displays the number of 
files in the directory.

 $ ls | wc -l

will show you how many files and directories in the current (target)
directory. To count just files, and exclude directories, you could try
something like

 $ find /target/directory -type f -print | wc -l


Except of course, that would descend into the subdirectories you're trying
not to count... Sorry - an object lesson in not hitting send before you've
tested what you scribbled.


ls -aF | grep -v /$ | wc -l

is a quick, if somewhat ugly, way to do it. It counts the dotfiles too.

Erik
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Determining the number of files in a directory

2007-11-03 Thread Daniel Larsson

On Sat, 2007-11-03 at 12:49 +, Daniel Bye wrote:
> On Sat, Nov 03, 2007 at 12:41:51PM +, Daniel Bye wrote:
> > On Sat, Nov 03, 2007 at 05:27:06AM -0700, White Hat wrote:
> > > This is probably a dumb question; however, I never let a little thing 
> > > like 
> > > that bother me in the past.
> > 
> > Heheh! You and many more, my friend, myself absolutely included!
> > 
> > > Using FreeBSD-6.2 and Bash, how do I determine the number of files in a 
> > > given directory? I have tried all sorts of combinations using different 
> > > flags with the 'ls' command; however, none of them displays the number of 
> > > files in the directory.
> > 
> >  $ ls | wc -l
> > 
> > will show you how many files and directories in the current (target)
> > directory. To count just files, and exclude directories, you could try
> > something like
> > 
> >  $ find /target/directory -type f -print | wc -l
> 
> Except of course, that would descend into the subdirectories you're trying
> not to count... Sorry - an object lesson in not hitting send before you've
> tested what you scribbled.

$ find /target/directory -maxdepth 1 -type f -print | wc -l

should do what you want though.

> 
> Dan
> 


signature.asc
Description: This is a digitally signed message part


Re: Determining the number of files in a directory

2007-11-03 Thread John Nielsen
On Saturday 03 November 2007, Daniel Bye wrote:
> On Sat, Nov 03, 2007 at 12:41:51PM +, Daniel Bye wrote:
> > On Sat, Nov 03, 2007 at 05:27:06AM -0700, White Hat wrote:
> > > This is probably a dumb question; however, I never let a little thing
> > > like that bother me in the past.
> >
> > Heheh! You and many more, my friend, myself absolutely included!
> >
> > > Using FreeBSD-6.2 and Bash, how do I determine the number of files in
> > > a given directory? I have tried all sorts of combinations using
> > > different flags with the 'ls' command; however, none of them displays
> > > the number of files in the directory.
> >
> >  $ ls | wc -l
> >
> > will show you how many files and directories in the current (target)
> > directory. To count just files, and exclude directories, you could try
> > something like
> >
> >  $ find /target/directory -type f -print | wc -l
>
> Except of course, that would descend into the subdirectories you're
> trying not to count... Sorry - an object lesson in not hitting send
> before you've tested what you scribbled.

find /target/directory -type f -maxdepth 1 | wc -l

should do the trick. See also man find and man wc, of course.

JN
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Determining the number of files in a directory

2007-11-03 Thread [LoN]Kamikaze
Daniel Bye wrote:
> On Sat, Nov 03, 2007 at 12:41:51PM +, Daniel Bye wrote:
>> On Sat, Nov 03, 2007 at 05:27:06AM -0700, White Hat wrote:
>>> This is probably a dumb question; however, I never let a little thing like 
>>> that bother me in the past.
>> Heheh! You and many more, my friend, myself absolutely included!
>>
>>> Using FreeBSD-6.2 and Bash, how do I determine the number of files in a 
>>> given directory? I have tried all sorts of combinations using different 
>>> flags with the 'ls' command; however, none of them displays the number of 
>>> files in the directory.
>>  $ ls | wc -l
>>
>> will show you how many files and directories in the current (target)
>> directory. To count just files, and exclude directories, you could try
>> something like
>>
>>  $ find /target/directory -type f -print | wc -l
> 
> Except of course, that would descend into the subdirectories you're trying
> not to count... Sorry - an object lesson in not hitting send before you've
> tested what you scribbled.
> 
> Dan
> 

Well just use the Unix tool for everything - grep:

$ ls -F | grep -Ev '/$'|wc -l

or if you also want to exclude symlinks:

$ ls -F | grep -Ev '/$|@$'|wc -l
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Determining the number of files in a directory

2007-11-03 Thread Matthias Apitz
El día Saturday, November 03, 2007 a las 05:27:06AM -0700, White Hat escribió:

> This is probably a dumb question; however, I never let a little thing like 
> that bother me in the past.
>  
> Using FreeBSD-6.2 and Bash, how do I determine the number of files in a given 
> directory? I have tried all sorts of combinations using different flags with 
> the 'ls' command; however, none of them displays the number of files in the 
> directory.
>  
> Other than, by writing a script to accomplish this feat, how could I achieve 
> my goal?

$ ls | wc -l
 293

matthias
-- 
Matthias Apitz
Manager Technical Support - OCLC PICA GmbH
Gruenwalder Weg 28g - 82041 Oberhaching - Germany
t +49-89-61308 351 - f +49-89-61308 399 - m +49-170-4527211
e <[EMAIL PROTECTED]> - w http://www.oclcpica.org/ http://www.UnixArea.de/
b http://gurucubano.blogspot.com/
___
freebsd-questions@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Determining the number of files in a directory

2007-11-03 Thread Daniel Bye
On Sat, Nov 03, 2007 at 12:41:51PM +, Daniel Bye wrote:
> On Sat, Nov 03, 2007 at 05:27:06AM -0700, White Hat wrote:
> > This is probably a dumb question; however, I never let a little thing like 
> > that bother me in the past.
> 
> Heheh! You and many more, my friend, myself absolutely included!
> 
> > Using FreeBSD-6.2 and Bash, how do I determine the number of files in a 
> > given directory? I have tried all sorts of combinations using different 
> > flags with the 'ls' command; however, none of them displays the number of 
> > files in the directory.
> 
>  $ ls | wc -l
> 
> will show you how many files and directories in the current (target)
> directory. To count just files, and exclude directories, you could try
> something like
> 
>  $ find /target/directory -type f -print | wc -l

Except of course, that would descend into the subdirectories you're trying
not to count... Sorry - an object lesson in not hitting send before you've
tested what you scribbled.

Dan

-- 
Daniel Bye
 _
  ASCII ribbon campaign ( )
 - against HTML, vCards and  X
- proprietary attachments in e-mail / \


pgpxwNPZjXDGB.pgp
Description: PGP signature


Re: Determining the number of files in a directory

2007-11-03 Thread Daniel Bye
On Sat, Nov 03, 2007 at 05:27:06AM -0700, White Hat wrote:
> This is probably a dumb question; however, I never let a little thing like 
> that bother me in the past.

Heheh! You and many more, my friend, myself absolutely included!

> Using FreeBSD-6.2 and Bash, how do I determine the number of files in a 
> given directory? I have tried all sorts of combinations using different 
> flags with the 'ls' command; however, none of them displays the number of 
> files in the directory.

 $ ls | wc -l

will show you how many files and directories in the current (target)
directory. To count just files, and exclude directories, you could try
something like

 $ find /target/directory -type f -print | wc -l

Dan

-- 
Daniel Bye
 _
  ASCII ribbon campaign ( )
 - against HTML, vCards and  X
- proprietary attachments in e-mail / \


pgpfFHUz0Kapy.pgp
Description: PGP signature