Introduction to UNIX for Web Developers The "wc" Utility The "wc" utility is used to count the number of words, characters or lines in a file and includes several useful options.

Option Explanation
-c Counts only characters
-l Counts only the number of lines (newline delimited)
-w Counts words only (defined as separated by a tab, space or newline)

Consider these examples of options in use

[wc Example]

Another cool way to use "wc" is to count the number of files in a directory by piping the output of "ls" to "wc" such as:

ls -l | wc -l

Finally, as with other commands, you can use the back tick (`) character to perform command substitution. Thus, the following command will count the number of files in the current directory and the one just above it. (Note you may have to subtract any header lines depending on your version of "ls" as in the version shown below in which we need to extract the lines total 5 and total 8)

expr `ls -l | wc -l` + `ls -l .. | wc -l`

[wc Complex Example]

Previous | Next | Table of Contents