Read in 3 minutes
Linux Wc Command
On Linux and Unix-like operating systems, the wc command allows you to count the number of lines, words, and bytes of each specified file or standard input and print the result.
In this tutorial, we will show you how to use the Linux wc command through simple and practical examples.
How to Use the Wc Command
The syntax for the wc command is as follows:
wc OPTION... [FILE]...
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 file name for each specified file. If no files are specified, ( when using the standard input) no file name is displayed.
The following command will display information about the virtual file
The output will look something like the following:
448 3632 22226 /proc/cpuinfo
448 is the number of lines, 3632 is the number of words, and 22226 is the number of characters.
When using the standard input, the file name is not shown:
wc < /proc/cpuinfo
448 3632 22226
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:
448 3632 22226 /proc/cpuinfo 49 143 1363 /proc/meminfo 497 3775 23589 total
The options below allow you to select which counts are printed.
--lines- Print the number of lines.
--words- Print the number of words.
--chars- Print the number of characters.
--bytes- Print the number of bytes.
--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:
wc -w /proc/cpuinfo
Here is another example that will print the number of lines and the length of the longest line.
wc -lL /proc/cpuinfo
448 792 /proc/cpuinfo
--files0-from=F option allows wc to read input from the files specified by NUL-terminated names in file
- 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:
find /etc -name 'host*' -printf0 | wc -l --files0-from=-
The output will show the number of lines for all files in the
/etc directory whose names start with “host”:
4 /etc/host.conf 27 /etc/avahi/hosts 1 /etc/hostname 14 /etc/hosts 46 total
Wc Command Examples
wc command is usually 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:
find . -type f | wc -l
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 .
getent passwd | wc -l
By now you should have a good understanding of how to use the Linux wc command.
If you have any question or feedback, feel free to leave a comment.