Linux wc Command
The Linux wc command is used to count words.
Using the wc command, we can calculate the number of bytes, words, or lines in a file. If no file name is specified, or if the provided file name is "-", the wc command will read data from the standard input device.
Syntax
wc [-clw][--help][--version][file...]
Parameters:
- -c or --bytes or --chars: Displays only the number of bytes.
- -l or --lines: Displays the number of lines.
- -w or --words: Displays only the number of words.
- --help: Provides online help.
- --version: Displays version information.
Example
By default, wc calculates the number of lines, words, and bytes in the specified file. The command used is:
wc testfile
First, view the contents of the testfile:
$ cat testfile
Linux networks are becoming more and more common, but security is often an overlooked
issue. Unfortunately, in today’s environment all networks are potential hacker targets,
from top-secret military research networks to small home LANs.
Linux Network Security focuses on securing Linux in a networked environment, where the
security of the entire network needs to be considered rather than just isolated machines.
It uses a mix of theory and practical techniques to teach administrators how to install and
use security applications, as well as how the applications work and why they are necessary.
Using wc to count, the result is as follows:
$ wc testfile # Statistics for the testfile
3 92 598 testfile # The testfile has 3 lines, 92 words, and 598 bytes
Here, the three numbers represent the number of lines, words, and bytes in the testfile, respectively.
To count information for multiple files, such as testfile, testfile_1, and testfile_2, use the following command:
wc testfile testfile_1 testfile_2 # Count information for three files
The output is as follows:
$ wc testfile testfile_1 testfile_2 # Count information for three files
3 92 598 testfile # First file has 3 lines, 92 words, and 598 bytes
9 18 78 testfile_1 # Second file has 9 lines, 18 words, and 78 bytes
3 6 32 testfile_2 # Third file has 3 lines, 6 words, and 32 bytes
15 116 708 total # Total for all three files: 15 lines, 116 words, and 708 bytes