Counting Files: How many MP3s do I have anyhow?

One can't rely on ls to produce a count like the DOS dir command does. Luckily, Linux is equipped with the wc command.

wc counts things fed to it on standard input. In this particular case, we're interested the number of lines being piped to it from ls, so we use wc -l to just count how many lines are being tossed at it. For example, to list the total number of MP3s in a directory, use:

ls -l *.mp3 | wc -l

To get recursive effects, you can pipe the output of the find command to it. Of course, there is no limit on what you can count with wc, being a nice UNIXy thing, you can apply it to all those other nice UNIXy things.