Type:
ls | wc -l
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 -lIf 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.
Once there have a look at Virgil Samms’ version as well.




