How To Count The Number Of Words In A File Using The Linux "wc" Command

woman using laptop on couch
PeopleImages.com/DigitalVision/Getty Images

Introduction

The Linux "wc" command can be used to provide a total of the number of words there are in a file. This is useful if you are trying to enter a competition which requires a maximum number of words or if you are a student with a minimum word limit requirement on an essay.

In truth this only really works well on text files but LibreOffice provides a "word count" option via the "tools" menu if you need the word count from a document with rich text such as a Word document, OpenOffice document or rich text file.

How To Use The "wc" Command

The basic usage of the "wc" command is as follows:

wc <filename>

For example I have a file called test.txt with the following contents:

My Essay
Title
The cat sat on the mat

To find out the number of words in this file I can use the following command:

wc test.txt

The output from the "wc" command is as follows:

3 9 41 test.txt

The values are as follows:

  • The first number is the number o f new lines characters
  • The second number is the number of words
  • The third number is the number of bytes
  • The final value is the file name

Get The Total Word Count From Multiple Files

You can provide multiple file names to the "wc" command as when you do you get the counts for each file and a total row.

To prove this I copied the test.txt file and called it test2.txt. To get the word count of both files I could run the following command:

wc test.txt test2.txt

The output is as follows:

3 9 41 test.txt

3 9 41 test2.txt

6 18 82 total

As before the first number on each line is the number of lines, the second number is the word count and the third number the total number of bytes.

There is another switch available which is a little bit strange in name and actually works in a fairly strange way.

The command looks like this:

wc --files0-from=-

(That is a zero after the word files)

When you run the above command you will see a cursor and you can enter a filename. Once you have entered the filename press CTRL and D twice. This will show the totals for that file.

Now you can enter another filename and press CTRL D twice. This will show the totals from the second file.

You can continue doing this until you have had enough. Press CTRL and C to exit back to the main command line.

The same command can be used to find the counts of all the words of all text files in a folder as follows:

find . -type f -print0 | wc -l --files0-from=-

This combines the find command with the word count command. The find command looks in the current directory (denoted by the .) for all files with a type of file and then prints out the name with a null character which is required by the wc command. The wc command takes the input and processes each file name returned by the find command.

How To Display Just The Total Number Of Bytes In A File

If you just want to get a count of the number of bytes in a file you can use the following command:

wc -c <filename>

This will return the total number of bytes and the filename.

How To Display Just The Total Number Of Characters In A File

The byte count is usually slightly higher than the total number of characters in a file.

If you want just the total character count you can use the following command:

wc -m <filename>

For the file test.txt the output is 39 and not 41 as it was before.

How To Display Just The Total Lines In A File

You can run the following command to return just the total number of lines in a file:

wc -l <filename>

 

How To Display The Longest Line In A File

If you want to know the longest line in a file you can run the following command:

wc -L <filename>

If you run this command against the "test.txt" file then the result is 22 which corresponds to the number of characters for the line "The cat sat on the mat".

How To Display Just The Total Number Of Words In A File

Last but not least, you can get the total number of words in a file by running the following command:

wc -w <filename>