UNIX / Linux wc Command Examples

What is wc ?
3 wc examples
Syntax and Options
Related Commands

What is wc ?

wc prints the number of lines, words and bytes in the given input. wc is the abbreviation of word count.

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.

Syntax:

$ 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

Related Commands

Comments on this entry are closed.

  • priyanka January 12, 2012, 1:10 am

    is there any command which tells the word count such as ‘printf’ in script?????suppose there is two ‘printf’ in one line then any specific command????????

  • Datta January 21, 2013, 1:45 am

    If we give more than one files to WC command .How it’s work?

    command : wc -l | ls -R /Datta

    Note:Datta is Directory…..