From owner-freebsd-questions@FreeBSD.ORG Sat Nov 3 21:42:15 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3DACB16A420 for ; Sat, 3 Nov 2007 21:42:15 +0000 (UTC) (envelope-from kline@tao.thought.org) Received: from tao.thought.org (dsl231-043-140.sea1.dsl.speakeasy.net [216.231.43.140]) by mx1.freebsd.org (Postfix) with ESMTP id B2BB413C4BF for ; Sat, 3 Nov 2007 21:42:14 +0000 (UTC) (envelope-from kline@tao.thought.org) Received: from tao.thought.org (localhost [127.0.0.1]) by tao.thought.org (8.13.8/8.13.1) with ESMTP id lA3LfkHA088241; Sat, 3 Nov 2007 13:41:46 -0800 (PST) (envelope-from kline@tao.thought.org) Received: (from kline@localhost) by tao.thought.org (8.13.8/8.13.1/Submit) id lA3LfjfE088240; Sat, 3 Nov 2007 13:41:45 -0800 (PST) (envelope-from kline) Date: Sat, 3 Nov 2007 13:41:45 -0800 From: Gary Kline To: cpghost Message-ID: <20071103214145.GB6857@thought.org> References: <538619.51984.qm@web34408.mail.mud.yahoo.com> <20071103124151.GA3207@torus.slightlystrange.org> <20071103124932.GB3207@torus.slightlystrange.org> <200711031044.43523.lists@jnielsen.net> <20071103210718.6b12f46e@epia-2.farid-hajji.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20071103210718.6b12f46e@epia-2.farid-hajji.net> User-Agent: Mutt/1.4.2.3i X-Organization: Thought Unlimited. Public service Unix since 1986. X-Of_Interest: With 21 of service to the Unix community. Cc: Daniel Bye , White Hat , John Nielsen , freebsd-questions@freebsd.org Subject: Re: Determining the number of files in a directory X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 03 Nov 2007 21:42:15 -0000 On Sat, Nov 03, 2007 at 09:07:18PM +0100, cpghost wrote: > On Sat, 3 Nov 2007 10:44:43 -0400 > John Nielsen wrote: > > > On Saturday 03 November 2007, Daniel Bye wrote: > > > On Sat, Nov 03, 2007 at 12:41:51PM +0000, 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 real 33.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 "freebsd-questions-unsubscribe@freebsd.org" -- Gary Kline kline@thought.org www.thought.org Public Service Unix http://jottings.thought.org http://transfinite.thought.org