Linuxstar
Published on

wc command-Count the number of bytes, words, and lines of a file

Jonas

Jonas

1 min read

The wc command counts the number of bytes, words, and lines in the specified file, and displays and outputs the statistical results. Using the wc command, we can calculate the number of bytes, words, or columns of the file. If the file name is not specified, or the given file name is "-", the wc command will read data from the standard input device. wc also gives the presidential count of the specified file.

Syntax: wc [parameter] [file]

Common parameters:

-wCount the number of words, or --words: display only the number of words. A word is defined as a string separated by whitespace, tabs, or newline characters
-cCount the number of bytes, or --bytes or --chars: only display the number of bytes
-lCount the number of lines, or --lines: display only the number of columns
-mCount the number of characters
-LPrint the length of the longest line
--help Display help information
--versionDisplay version information
Example

Count words:

[root@linuxstar ~]# cat test.txt hello world hello world hello world hello world hello world [root@linuxstar ~]# wc -w test.txt 10 test.txt

Statistics bytes:

[root@linuxstar ~]# wc -c test.txt 60 test.txt

Count the number of characters:

[root@linuxstar ~]# wc -m test.txt 60 test.txt

Statistics rows:

[root@linuxstar ~]# wc -l test.txt 4 test.txt

Print the length of the longest line:

[root@linuxstar ~]# wc -L test.txt 23 test.txt