Special File Types   «Prev 

Word Count Command in Unix

wc, head, and tail Commands used with Special File Types

The wc command gives you a line, word, or character count of a file. You can use the –l option for a line count, the –c option for a character count, and the –w option for a word count. For example:

wc –l file1

wc wc [options] [files]

Word count. Print a character, word, and line count for files. If multiple files, print totals as well. If no files are given, read standard input.

Common Options

  1. -c, --bytes: Print byte count only.
  2. -l, --lines: Print line count only.
  3. -m, --chars: Print character count only. This will be different than -c in a multibyte character environment.
  4. -w, --words: Print word count only.

Solaris Option

-C Same as -m.

GNU/Linux Option

-L, --max-line-length
Print length of longest line.
Examples
1. Count the number of users logged in:
who | wc -l

2.Count the words in three essay files:
wc -w essay.[123]

3. Count lines in file named by $file (do not display filename):
wc -l < $file

Unix System Administration
would output the number of lines in the file named file1.
The head and tail commands allow you to display the first (head) or last (tail) part of a specified file. The default amount to be displayed is 10 lines. These commands read from standard input if no files are given or when a filename of - is encountered.
For example:
head –15 file1

would display the first 15 lines of the file named file1 and
tail file2

would display the last 10 lines of the file named file2.