Counting Made Easy
Have you ever tried to answer a “25 words or less” quiz? Did you ever have to write a 1500-word essay? With the wc you can easily verify that your contribution meets the criteria. The wc command counts the number of characters, words, and lines. It will take its input either from files named on its command line or from its standard input. Below is the command line form for the wc program:
$ wc [-c|-l|-w] [filename...]
|-c||Compute character count.|
|-l||Compute line count.|
|-w||Compute word count.|
|filename||Filename to be counted. If no filename is not specified, then the text will be read from the standard input. For clarity, the filename will be written as the last line of each counting report, even if only one filename is used.|
When used without any command line switches, wc will report on the number of characters, lines, and words. Command line switches can be combined to return any combination of character count, line count or word count.
Example 1. Counting Characters
To count the characters in a file, just run wc -c:
$ echo hello | wc -c 6
In addition to the five letters in the word, the line also has a NL character at the end.
Example 2. Invisible Characters Are Important, Too
Characters that you cannot see still occupy space in a file.
$ echo Hello, World! > greetings $ wc -c greetings 14
Keep in mind that spaces and TABs count as characters, too. Remember our typewriter analogy? Both the spacebar and the TAB key require keystrokes; each character in a text file corresponds to a press of a typewriter key.
Example 3. What’s My Line?
Run the command wc -l to count the lines in a file:
$ echo First line > foo $ echo Second line >> foo $ echo Third line >> foo $ wc -l foo 3 foo
Example 4. I Want It All
Using wc without any arguments counts everything: characters, words, and lines:
$ echo one > x $ echo two words >> x $ echo three more words >> x $ wc x 3 6 31 x
Example 5. Counting Users
The wc command is often used to count the number of things, not just lines, words, and characters. For example, the users command generates a list users who are currently logged onto the machine. The following line would create an alias called nusers, which would report the number of currently logged on users.
$ alias nusers=’users | wc -w’ $ users student student student student root $ nusers 5
Example 6. Counting Processes
By examining the output of a command such as ps aux, which prints information about one process per line, the wc -l command can be used to count the number of processes currently running on a machine. Examining the output of the ps aux command, however, the initial line, which contains the column titles, must be removed from the count.
$ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 2.4 0.7 179740 14024 ? Ss 03:04 0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 18 root 2 0.0 0.0 0 0 ? S 03:04 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< 03:04 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< 03:04 0:00 [rcu_par_gp] root 5 0.0 0.0 0 0 ? I 03:04 0:00 [kworker/0:0-events] root 6 0.0 0.0 0 0 ? I< 03:04 0:00 [kworker/0:0H-xfs-log/nvme0n1p2] root 7 0.0 0.0 0 0 ? I 03:04 0:00 [kworker/u4:0-events_unbound] ....
The tail command, with it’s ability to print the remainder of a file starting from a specified line, can be used to remove the header line.
$ ps aux | tail +2 root 1 2.4 0.7 179740 14024 ? Ss 03:04 0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 18 root 2 0.0 0.0 0 0 ? S 03:04 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? I< 03:04 0:00 [rcu_gp] root 4 0.0 0.0 0 0 ? I< 03:04 0:00 [rcu_par_gp] root 5 0.0 0.0 0 0 ? I 03:04 0:00 [kworker/0:0-events] root 6 0.0 0.0 0 0 ? I< 03:04 0:00 [kworker/0:0H-xfs-log/nvme0n1p2] root 7 0.0 0.0 0 0 ? I 03:04 0:00 [kworker/u4:0-events_unbound] ....
The following short script combines ps aux, tail +2, and wc, to create a new command called nprocs. When made executable, and placed the ~/bin directory (which is part of the standard executable search PATH), the script becomes available from the command line.
$ cat nprocs #!/bin/bash ps aux | tail +2 | wc -l
$ mkdir bin $ mv nprocs bin $ chmod a+x bin/nprocs $ nprocs 86