Counting files and their contents with wc
One of UNIX's handiest capabilities lets you count files and their contents. For example, you can count the number of files in a directory, or you can count the number of words or lines in a file. You do this counting with the wc command, as shown in Code Listing 6.1.
To count words using wc:
- At the shell prompt, type wc -w (for words) and the name of the file you want to count the words in. wc will oblige, as shown in Code Listing 6.1.
wc -w honeydo
To count lines with wc:
- Use wc -l followed by the file name to count the lines in the file (Code Listing 6.2). This is useful for poetry or for things like lists (e.g., our "honey-do" list always has a minimum of 73 items on it).
wc -l honeydo
Tip
You can ...
Get Unix: Visual QuickStart Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.