The output is an approximation of how many files are in the current directory.
The command ls gives a list of files (ls stands for “list stuff”)

the vertical line is a pipe. This means the standard output of the left side of the pipe is sent (like in a pipe) to the standard input of the right side.

wc means “word count” … the default output is the number of lines, nmber of words, and number of bytes for a file or for standard input that was sent to the command. The -l option puts out only the number of lines. That, then, is the number of files.

The last time I mentioned this, commenter Colin M added this:

And for recursive (list the number of files under the current directory and all subdirectories, etc):

find | wc -l

If you want to count just normal files (i.e. not directories and other weird things):

find -type f | wc -l

I mention above that this is an approximation. It is approximate because there are all kinds of screwey things that can happen to make this the incorrect number. A better command that will probably give you the exact number of files in the current directory is this:

find . -type f -maxdepth 1 -printf "%i\n" | sort | uniq | wc -l

Gaze at this bit of code for a while and try to figur out why this works.

The first part requires that find finds files in the present directory (the dot makes that happen) that really are files (type f) and ignores anythying in any subdirectories (maxdepth). After that, it gets obscure. To find out why this works the way it does, examine the comment provided by Master Basher Winter Toad.

Comments

You can also specify only directories or (and I’ve found this useful when hunting a virus) only hidden files by adding “/a d” or “/a h” before the pipe. Or control the subdirectory depth with /s. It doesn’t seem to have the /same/ problem with erroneous counts, but occasionally gets mixed up on other things instead.

And of course, under both you can pipe the output to a file or executable for monitoring/datalogging purposes.

I’ve tried from time to time to get a handle on Bash, but tcsh remains my favorite shell. Here’s a line from my tcshrc file:

alias list “ls -lA; echo ‘total items this dir:'; ls -lA | wc -l”

(That’s all one line.)
Bash just spits this back at me with “not found”. I think it wants me to do something different with the quotes. Most likely, something similar could be offered to Bash that would be accepted, and you’d get a directory listing and the number of entries at the bottom.

John, the reason your “alias” command gave you a “not found” message is because the usage for the built-in alias command (in bash) has this basic usage:

alias [name[=command] …]

So if you give it alias, you’ll get a list of all aliases already defined.

If you give it alias ls you’ll see the current alias for the ls command.

If you give it alias ls vi you’ll see the aliases for both the ls and vi commands. For example, on my system:

$ alias ls vi
alias ls=’ls –color=tty’
alias vi=’vim’

So if I type:

$ alias ls “ls -lA”

..then it tries to give me the definitions of the ls alias, and (standard UNIX quote rules apply) the ls -lA alias. But I don’t have an alias called ls -lA so it barfs with a “not found” for that alias:

Well, of course, in Linux with gnome, I can see that too. However, I don’t think you see that on Windows.

But, of course, on needs to have the number as output for piping it to the next script. For some reason. Which, by the way, is why the longer listing example above is not really what we are looking for. We just want the number.

John: Try zsh. csh and its variants are a bad idea. The bugs of csh may have been fixed but the design flaws remain.

I’m wary of aliases especially on machines I’m not familiar with (when using shared accounts, etc.) so I prefer using the full path in certain cases. It may not be as important now, but older Solaris machines would keep a separate copy of BSD-flavoured commands under /usr/ucb so depending on how weirdly paths were set up, you might get the BSD version of ps instead of the expected AT&T version.

And finally, some commands behave differently if the output is sent somewhere besides the terminal. For example, ‘ls –color=auto’ will send ANSI color escape sequences to the terminal but will omit them when the output is sent to a file or piped into a command.

In this case, on a Linux system I’d use ‘/bin/ls -A1 | wc -l’ The -A option tells it to show all the hidden files and directories – anything with a leading dot except . and .. (the current and parent directories.) The -1 option puts one entry per line so wc is counting what you think it’s counting.

The upshot is that sometimes you need to be very precise in what you tell the machine to do and the only way to know how precise to be is to make mistakes. The unix world is a rich tapestry of obscure, archaic, and powerful tools and it takes equal parts patience, experimentation, and RTFMing to develop proficiency. It’s a lot better now than when I started with it; disk is cheap enough that documentation is now stored online instead of that foot-thick binder with tattered pages bolted to the desk in the computer lab. RTFMing is a lot easier now.

Greg: are you running windows on a 1950’s industrial punch press or something? Your computer probably shouldn’t be making those noises if not. If so, well, I knew windows supported some pretty antiquated hardware, but dang…

(at any rate piping a directory listing to ‘find’ works great. So neener.)

This is too funny. I just got an IM from a coworker asking “Do you know how to get a count of files in a particular directory?” I sent back “ls -1a | wc -l”. The next thing she wanted was a count of all the files in all the sub-directories. Hah!

You don’t need the -1 option, Ben. ls knows that it is writing to a pipe rather than a (pseudo)terminal so it automatically switches to that format (one file per line, no funny escape codes to colour code the files).

-a also lists files that begin with ‘.’ (those are normally hidden in Unix — we don’t have a separate file flag for that the way they do in DOS/Windows). But ‘.’ and ‘..’ (current and parent directories) also begin with a ‘.’. Do you want to include them in the count?

By Other Blog Is…: The X Blog

According to a source cited by NPR the Ferguson Police Department … violated the Constitution when it policed to raise money and with a racial bias toward African-Americans, according to a law enforcement official familiar with the report. The investigation, the source says, concluded that blacks were disproportionately targeted by the police and the justice […]

“Please Don’t Paint Our Planet Pink!: A Story for Children and their Adults” is a new children’s book by Gregg Kleiner about global warming. The idea is simple. Imagine if you could see CO2? In the book, it is imagined to be pink. The imagining takes the form of a quirky father, one imagines him […]

“You can believe the United States Navy or you can believe the Senator with the snowball.” “You can believe the Pope or you can believe the Senator with the snowball.” “We can believe [great American corporations] or we can believe the Senator with the snowball.” “You can believe every single major American scientific society, or […]