What is wc ?
3 wc Examples
Print the number of lines, words and characters in a file
To view the number of lines, words and characters in a file, give the filename as argument to the wc command as shown below.
$ wc filename X Y Z filename
- X – Number of lines
- Y – Number of words
- Z – Number of bytes
- filename – name of the file
$ wc demofile.txt 36 104 570 demofile.txt
The above output indicates that the demofile.txt has 36 lines, 104 words, and 570 bytes.
Print specific counts as required
You can use -l option to print only the line count as shown below.
$ wc -l demofile.txt 36 demofile.txt
The same way you can use the following options for specific requirements such as,
- -w : print only word count
- -c : print only the byte count
You can also use any combination of above options.
Print the length of longest line
You can use -L option to print the length of longest line in the file.
$ wc -L demofile.txt 84 demofile.txt
Syntax and Options
|Short Option||Long Option||Option Description|
|-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|
|-L||–max-line-length||print the length of the longest line|
|-w||–words||print the word counts|