Determining the number of files in a directory

Pietro Cerutti gahr at
Sat Nov 3 15:25:37 PDT 2007

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


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


> Thanks!

Pietro Cerutti

PGP Public Key:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 187 bytes
Desc: OpenPGP digital signature
Url :

More information about the freebsd-questions mailing list