Linux wc command

Updated: 06/16/2017 by Computer Hope

About wc

wc, or "word count," prints a count of newlines, words, and bytes for each input file.


wc prints newline, word, and byte counts for each FILE, and a total if more than one FILE is specified. With no FILE, or when FILE is a dash ("-"), wc operates on standard input. (A word is a non-zero-length sequence of characters delimited by white space.)

The options below may be used to select which counts are printed. Counts are always in the following order: newline, word, character, byte, maximum line length.

wc syntax

wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F


-c, --bytes print the byte counts.
-m, --chars print the character counts.
-l, --lines print the newline counts.
--files0-from=F read input from the files specified by NUL-terminated names in file F; If F is "-" then read names from standard input.
-L, --max-line-length print the length of the longest line.
-w, --words print the word counts.
--help display a help message, and exit.
--version output version information, and exit.

wc examples

wc myfile.txt

Displays information about the file myfile.txt. Output will resemble the following:

5 13 57 myfile.txt

Where 5 is the number of lines, 13 is the number of words, and 57 is the number of characters.

ls -1 | wc -l

This command returns the number of objects in the current directory. It uses the ls command to produce a single-column (-1) listing of the directory contents, which outputs one line per object; this output is piped to wc, which counts the lines (-l), and returns that number.

cksum — Calculate and display a CRC for files.
nl — Number the lines in a file.