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

DonJr djr1952 at hotpop.com
Thu Jan 10 21:29:46 EST 2008


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.

> 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.





More information about the Ma-linux mailing list