Counting Chars/Words/Lines with wc
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.