Counting Chars/Words/Lines with wc

NOTE: this website is under construction for the new quarter. Please pardon our dust! The content of this website is subject to change as we put together resources for the new quarter. If you are a student who took the course last quarter, you should visit last quarter's class web site instead, accessible by visiting this link.

Written by Chris Gregg, with modifications by Nick Troccoli

Click here for a walkthrough video.

The wc ("word count") command counts the lines, words, and characters in a file:

$ wc hello.c
 7 10 98 hello.c
$ cat hello.c
#include<stdio.h>
#include<stdlib.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

The hello.c file above has 7 lines, 10 words, and 98 characters.

The wc command can be useful if you need to count any output of a file, and it is frequently used by piping the output from a command or program:

$ ls -1 | wc
7 7 71
$

There are 7 files in the directory above.