[Ma-linux] Re: [Novalug] How to get the number of files in a given directory

Clif Flynt clif@cflynt.com
Thu Jan 10 21:47:48 EST 2008


On Thu, Jan 10, 2008 at 09:14:34PM -0500, Mike Shade wrote:
> Janina Sajka wrote:
> >geoffm writes:
> >  
> >>ls /usr/local/bin/ | wc -l
> >>    
> >
> >
> >No, that won't work. You're printing multiple filenames per line. You
> >need the '-1' argument to ls in order to rely on wc counting the number
> >of lines correctly.
> >
> >  
> Janina,
> 
> Though when referenced directly ls tends to output multiple per row, 
> when piped it actually does  list one per line.  I dont' know the rhyme 
> or reason to this, or whether it's configuration or GNU version 
> dependent, but it does work that way.

  This feature has existed in ls since the early days of Unix.  It was
an initial design decision of Kernighan, Plauger, Ritchie, Aho, et al.

  The reason is so that you can pipe the output of one program
into another.  The idea was that all Unix commands would print
only real data (no header info, etc), so that they could easily
me combined into larger applications.

  The number of shell scripts, perl scripts, and even Tcl scripts
that execute an OS command, grab the output, and chunk on it attests
to their cleverness.

  I don't know if "Software Tools" is still in print, but it's a 
great read for the Unix developer or sysop, even if a lot of it
revolves around processing Fortran code.

Clif

-- 
.... Clif Flynt ... http://www.cflynt.com ... clif@cflynt.com ...
.. Tcl/Tk: A Developer's Guide (2nd edition) - Morgan Kauffman ..
.. 15'th Annual Tcl/Tk Conference:  Oct 2008,  Philadelphia, PA.. 
.............  http://www.tcl.tk/community/tcl2008/  ............








More information about the Novalug mailing list