This tutorial explains Linux “wc” command, options and its usage with examples.
The program reads either standard input or a list of files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow. If a file is not specified for the File parameter, standard input is used. The command writes the results to standard output and keeps a total count for all named files. If flags are specified, the ordering of the flags determines the ordering of the output. A word is defined as a string of characters delimited by spaces, tabs, or newline characters.
wc [-c | -m | -C ] [-l] [-w] [ file … ]
Same as -m.
Count words delimited by white space characters or new line characters.
Name of file to word count.
1. Displaying Line,word and character count in myfile.txt.
$ wc myfile.txt 5 13 57 myfile.txt
5 = Lines
13 = Words
57 = Characters
2. Count how many files and directories are in the current directory
$ ls -1 | wc -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.
3. Display Length of Longest Line
$ wc -L abc
The ‘wc‘ command allow an argument ‘-L‘, it can be used to print out the length of longest (number of characters) line in a file. So, we have the longest character line (‘Scientific Linux‘) in a file.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.