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

Janina Sajka janina at rednote.net
Fri Jan 11 14:21:01 EST 2008


DonJr writes:
> On Thu, 2008-01-10 at 20:54 -0500, 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.
> 
> Are you assuming "multiple filenames per line" because that's how it
> displays in your terminal?
> 
> If so then try:
>   /bin/ls /usr/local/bin/ | less
> 
> to see what the program 'wc' is really getting as input.
> 
>   { The full path to 'ls' is in order to override/bypass any shell
> alias. }
> 
> You will find that when 'piped' 'ls' by default puts out one file name
> per line unless you "override" that with an option.
> 
OK. I learned something. Thanks.
 I'm so accustomed to 'ls -1 |more' because it's just easier for me to
handle that output. Indeed, when I do multiple columns, I do them 'ls
-x' just to get more understandable TTS readout. Makes sense to have the
pipe behavior different, though.

Janina

> > However, your version counting words would do it:
> > 
> > ls /usr/local/bin/ |wc -w
> > However, this one is a problem if it's at all possible that you'd have
> > filenames with spaces, a la Windows.
> 
> Not sure where that would ever return useful information relating to the
> number of files in a directory.
> 
> -- DonJr.
> 

-- 

Janina Sajka,	Phone:	+1.202.595.7777;	sip:janina at a11y.org
Partner, Capital Accessibility LLC	http://CapitalAccessibility.Com

Marketing the Owasys 22C talking screenless cell phone in the U.S. and Canada
Learn more at http://ScreenlessPhone.Com

Chair, Open Accessibility	janina at a11y.org	
Linux Foundation		http://a11y.org



More information about the Ma-linux mailing list