How To Use Wc Command (Count Number Of Lines, Words, And Characters)
In this tutorial, we will show you how to use the wc command through simple and practical examples.
How to Use the wc Command
The syntax for the wc command is as follows:
The wc command can accept zero or more input FILE names. If no FILE is specified, or when FILE is
-, wc will read the standard input. A word is a string of characters delimited by a space, tab, or newline.
In it’s simplest form when used without any options, the wc command will print four columns, the number of lines, words, byte counts and the name of the file for each file passed as an argument. When using the standard input the fourth column (filename) is not displayed.
For example, the following command will display information about the virtual file /proc/cpuinfo:
The output will look something like the following:
448 is the number of lines.
3632 is the number of words.
22226 is the number of characters.
When using the standard input, the file name is not shown:
To display information about more than one file, pass the filenames, as arguments, separated by space:
The command will give you information about each file and a line including total statistics:
The options below allow you to select which counts are printed.
-l, –lines – Print the number of lines.
-w, –words – Print the number of words.
-m, –chars – Print the number of characters.
-c, –bytes – Print the number of bytes.
-L, –max-line-length – Print the length of the longest line.
When using multiple options counts are printed in the following order: newline, words, characters, bytes, maximum line length.
For example, to display only the number of words you would use:
Here is another example that will print the number of lines and the length of the longest line.
The –files0-from=F option allows wc to read input from the files specified by NUL-terminated names in file F. If F is
- then read names from standard input. For example, you can search for files using the find command and provide those files as an input to wc:
The output will show the number of lines for all files in the /etc directory whose names start with “host”:
Count the Number of Lines
The wc command is mostly used with the -l option to count only the number of lines in a text file. For example, to count the number of lines in the /etc/passwd file you would type:
The first column is the number of lines and the second one is the name of the file:
Count the Number of Words
To count only the number of words in a text file use wc -w followed by the file name. The following example counts the number of words in the ~/Documents/file.txt file:
The number of words is shown in the first column:
Wc Command Examples
The wc command can be used in combination with other commands through piping. Here are a few examples.
Counting Files in the Current Directory
The find command passes a list of all files in the current directory with each file name on a single line to the wc command, which counts the number of lines and prints the result:
Count the number of users
In the example below wc is used to count the number of lines from the output of the getent command .
The wc command stands for “word count” and has a quite simple syntax. It allows you to count the number of lines, words, bytes, and characters in one or multiple text files.