How do I count lines, words, and bytes in a file?
Author: Deron Eriksson
Description: This Linux tutorial demonstrates how to count the lines, words, and bytes in a file.
Tutorial created using: CentOS release 4.6
The "wc" command can be used to count the newlines, words, and bytes in a file. If no file is specified, it reads from standard input.
Here are several commands utilizing "wc":
wc commons-lang-2.4.pom cat commons-lang-2.4.pom | wc wc -l commons-lang-2.4.pom wc -w commons-lang-2.4.pom wc -c commons-lang-2.4.pom
The first command, "wc commons-lang-2.4.pom", displays the number of newlines, words, and bytes in the commons-lang-2.4.pom file. There are 462 newlines, 698 words, and 13976 bytes. The next command pipes the results of "cat commons-lang-2.4.pom" to wc so that wc reads the contents of the pomW file from standard input. Following this, we display just the number of newlines via the -l switch. This is followed by just the number of words via the -w switch. Last of all, we display the number of bytes via the -c switch.
The results of all these commands are shown here: