Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 03 Nov 2007 14:35:01 +0100
From:      Erik Cederstrand <erik@cederstrand.dk>
To:        FreeBSD Users Questions <freebsd-questions@freebsd.org>
Subject:   Re: Determining the number of files in a directory
Message-ID:  <472C7905.5020307@cederstrand.dk>
In-Reply-To: <20071103124932.GB3207@torus.slightlystrange.org>
References:  <538619.51984.qm@web34408.mail.mud.yahoo.com>	<20071103124151.GA3207@torus.slightlystrange.org> <20071103124932.GB3207@torus.slightlystrange.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Daniel Bye wrote:
> On Sat, Nov 03, 2007 at 12:41:51PM +0000, 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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?472C7905.5020307>