Linux wc utility

linux wc utility

How can I see the number of lines in a file? How to count the number of words in a file? Or how to find out the size of the file in bytes? On linux, all this can be found with one single command – wc.

Description of the wc utility

The Linux wc utility displays the newline, word, and byte counts in the specified file. Full documentation of the wc utility can be viewed at site.

Using the command

For example, let’s create a test file:

$ nano test.txt

And add the following content to it:

word1
word2
word3

Now let’s try using the wc utility:

$ wc test.txt

 3  3 18 test.txt

Consider the resulting output:

  • First 3 – number of lines in file
  • Second 3 – number of words in file
  • 18 – number of byts in file

The linux wc utility can accept several files as input, printing in the output not only information about each of them, but also general data:

$ wc test.txt test.txt 
 3  3 18 test.txt
 3  3 18 test.txt
 6  6 36 total

Command parameters

Besides the standard output, you can use the following parameters to modify it:

-с, –bytes – print the byte counts in file:

$ wc -c test.txt 
18 test.txt

-m, –chars – print the character counts in file:

$ wc -m test.txt 
18 test.txt

-l, –lines – print the newline counts in file:

$ wc -l test.txt 
3 test.txt

-L, –max-line-length – print the maximum display width in file:

$ wc -l test.txt 
5 test.txt

-w, –words – print the word counts in file:

$ wc -l test.txt 
3 test.txt

–help – display this help and exit

–version – output version information of linux wc utility and exit

Examples of using

The wc utility can be used to count the output of other commands, for example, to count matches:

$ cat test.txt | grep word2 | wc
      1       1       6

You can find even more useful Linux utilities on this page.