Linux and Unix wc command

Quick links

About wc
Syntax
Examples
Related commands
Linux and Unix main page

About wc

Short for word count, wc displays a count of lines, words, and characters in a file.

Syntax

wc [-c | -m | -C ] [-l] [-w] [ file ... ]

-cCount bytes.
-mCount characters.
-CSame as -m.
-lCount lines.
-wCount words delimited by white space characters or new line characters. Delimiting characters are Extended Unix Code (EUC) characters from any code set defined by iswspace()
fileName of file to word count.

Examples

wc myfile.txt

Displays information about the file myfile.txt. Below is an example of the output.

5    13    57   myfile.txt

5 = Lines
13 = Words
57 = Characters

ls -1 | wc -l

Count how many files and directories are in the current directory. To prevent any confusion, the above command reads ls <dash><the #1> <pipe> ls <dash><the letter l>. This command uses the ls command to list files in a bare format and pipes the output into the wc command to count how many files are listed. When done properly, the terminal should return a single number indicating how many lines were counted and then return you to the prompt.

Tip Keep in mind that this is also counting the ./ and ../ directories.

Related commands

cksum