Wednesday, August 01, 2001

Word Count in a file

Another command related to the contents of files is wc. It is used to count the number of lines, words and characters in a file. If you use it like this example you will get the number of lines, words and bytes (characters in a "text" file) of a file

wc file-name

For getting the result of only one of the three possible outputs above you have to give some option:

  1. For lines output:

    wc -l file-name
  2. For words output:

    wc -w file-name
  3. For bytes (characters in a "text" file:

    wc -c file-name


    wc -w file-name

Another interesing option allows you to get the length of the longest line in the file:

wc -L file-name

You can give more than one file name in your command line; the output will have the file name appended to the results of the counting.