wc Command in Linux

Back

The wc command, short form of word count. can return the number of lines, words, and charcters in a file. The wc options are straightforward .


Syntax :

wc [-c | -m | -C ] [-l] [-w] [ file ... ]

-c Count bytes.
-m Count characters.
-C Same as -m.
-l Count lines.
-w Count words delimited by white space characters or new line characters. 

For examples :

$ 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